I’m trying to pretty closely mimic the functionality of the iPhone phone app recent calls TableView, using an NSTableView on OS X. The desired functionality is that you click a button and the TableView switches from showing you all the recent calls to showing the subset of those calls that were missed, AND, I want to have the animation that you see on the iPhone where the appearance and disappearance of the rows is animated.
I’m using a view based NSTableView and I have it wired up very simply with a manual data source and delegate in my view controller (not using cocoa bindings).
Everything is working except when trying to delete rows to show the subset of “missed” calls. If I just reconstruct the NSMutableArray that is the data source for the TableView and then call reloadData, it works perfectly. But that gives me no animation. Here is the unanimated version which results in the TableView and the data source NSMutableArray in perfect sync, the correct content in the TableView, but no animation:
- (IBAction)showMissed:(id)sender {
NSMutableIndexSet *indices = [NSMutableIndexSet indexSet];
Call *call;
for (call in callsArray) {
if (!call.missed) {
[indices addIndex:[callsArray indexOfObject:call]];
}
}
if ([indices count] > 0) {
[callsArray removeObjectsAtIndexes:indices];
[self.recentsTableView reloadData];
}
}
When I try to use the NSTableView removeRowsAtIndexes:indices withAnimation: method to get the animation I want, I am getting some very strange behavior. Here is the animated showMissed method:
- (IBAction)showMissed:(id)sender {
NSMutableIndexSet *indices = [NSMutableIndexSet indexSet];
Call *call;
for (call in callsArray) {
if (!call.missed) {
[indices addIndex:[callsArray indexOfObject:call]];
}
}
if ([indices count] > 0) {
[callsArray removeObjectsAtIndexes:indices];
[self.recentsTableView removeRowsAtIndexes:indices withAnimation:NSTableViewAnimationSlideUp];
}
}
The logic I am using in the animated version of the method is pulled out of Apple’s TableViewPlaygound sample code.
When I run this code, I get two strange results. First, the TableView does not show only “missed” calls — it shows the right number of rows (i.e. the number of rows in the TableView equals the number of Call objects marked as missed), but the actual objects showing in the rows are some missed and some not missed.
Even weirder though is that after the animated version runs, the callsArray has ALL of the Call objects in it, as though the [callsArray removeObjectsAtIndexes:indices]; statement was not there at all. It seems like somehow the removeRowsAtIndexes:indices withAnimation: method is messing with the contents of my data source array and throwing the whole thing out of whack.
FWIW, my animated showAll method which is basically the reverse of the above using the insertRowsAtIndexes:indices withAnimation: seems to work perfectly.
What am I doing wrong?
There must have been some corruption somewhere in the sync between the data source array and the tableView. I fixed it by adding a reloadData call at the top of the method. Here is the fixed showMissed method: