I am making a UITableView with a UISegmented control ala the Recent Calls tab in the Phone app.
The first segment will be all items, the next 2 will be subsets of the full list of items.
I created 3 arrays which hold the 3 different lists of data.
My first solution was a wholesale swap of the data and the a reload:
[self setData:newData];
[self.tableView reloadData];
This works great with the exception of animation, there are no indicators that the data has indeed changed. I could add some sort of indicator message, but I would rather use the method employed by the phone app, an "accordion" effect collapsing deleted cells and expanding added cells.
To accomplish this, you cannot just swap out the data (or can you?). It doesn't trigger animation. The only means I see is to do this is manually delete and add cells from the backing model. So what is an efficient algorithm for accomplishing this? My first guess:
I have a multidimensional array to represent sections of cells.
- create an array to hold the index paths of cells to be added
- iterate through each section of the NEW data
- check if the section of the NEW data has the same count as the OLD data
- if not, iterate over each item in the NEW data
- If the item is also in the OLD data, do nothing
- If the item is not in the OLD data, add index path of item to array of cells to be added
- repeat for each item in section
- repeat for each section in NEW data
- create an array to hold the index paths of cells to be deleted
- iterate through each section of the OLD data
- check if the section of the NEW data has the same count as the OLD data
- if not, iterate over each item in the OLD data
- If the item is also in the NEW data, do nothing
- If the item is not in the NEW data, add index path of item to array of cells to be deleted
- repeat for each item in section
- repeat for each section in OLD data
then call the apprpriate UITableView methods:
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
This algorithm seems like has lots of room for improvement. Has anyone tackled this problem yet? How did you implement it?
Thanks for any input