Setting up the model for UITableView data to be filtered with UISegmentedControl
I am making a UITableView with UISegmented control ala Recent Calls tab in Phone app.
The first segment will have all the elements, and the next 2 will be subsets of the full list of elements.
I have created 3 arrays that contain 3 different lists of data.
My first solution was bulk data swap and reboot:
[self setData:newData];
[self.tableView reloadData];
This works great, except for the animation, there are no indicators that actually changed the data. I could add some kind of indication message, but I would prefer to use the method used in the phone app, the "accordion" effect breaking the deleted cells and expanding the added cells.
To accomplish this, you cannot simply change the data (or can you?). It doesn't trigger animation. The only thing I can see is to do it manually remove and add cells from the support model. So what is the efficient algorithm for this? My first guess:
I have a multidimensional array to represent sections of cells.
- create an array to store the index of the path to the cells to be added
- iteration through each section NEW data
- check if the NEW data section has the same account as the OLD data
- if not, repeat on each item in NEW data
- If the item is also in OLD data, do nothing
- If item is not in OLD data, add item index path to cell array added
- repeat for each item in the section
- repeat for each section in NEW data
- create an array to store the index of the path of the deleted cells
- iteration over each OLD section
- check if the NEW data section has the same account as the OLD data
- if not, repeat on each item in the OLD data
- If the item is also in NEW data, do nothing
- If item is not in NEW data, add item index path to cell array with strikeout
- repeat for each item in the section
- repeat for each section in OLD data
then call the appropriate UITableView methods:
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
This algorithm seems to have a lot of room for improvement. Has anyone else solved this problem? How did you implement it?
Thanks for any input
a source to share
I answered a similar question on the following SO page:
Template for updating UItableView and its data source after filtering with NSPredicate
a source to share
It would seem that this is more difficult than necessary. UITableViews work on a "pull" basis, that is, instead of pushing data onto a table, it calls the data source delegate and inserts data into it.
Considering that every time you call reloadData
you can swap and return whatever you want. All you have to do is use segmented control actions to change the value of the index, which selects which dataset is "current", which is used when the table asks for a cell value.
Now, in terms of change animation, there are several ways you could have the same effect. If you really want to make an accordion effect, you will need to manage a lot of states to keep track of whether you are in display mode or table mode, etc. An easier way is to have an overlay view that you could animate in and visually indicate the change. Then, in the animation preview completion handler, you call reloadData
.
a source to share