1

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?

5
  • Don't edit the title to say "solved" - answer your own question instead.
    – jrturton
    Jun 3, 2012 at 18:40
  • I did both. Does it not help to see "SOLVED" in the title?
    – pjv
    Jun 3, 2012 at 18:42
  • No, accept your answer instead (I know it takes a while before you're allowed to do that, but that's the best thing to do)
    – jrturton
    Jun 3, 2012 at 19:09
  • I will accept my answer when I am allowed to after two days. FWIW, I disagree with you about editing the question title; when I am searching through questions on SO or google, seeing SOLVED conveys information that I find useful in scanning through the results of a search.
    – pjv
    Jun 3, 2012 at 23:25
  • It would make a total mess of the front page, there is a visual indicator that questions are solved. Also, meta.stackexchange.com/a/131016/168534
    – jrturton
    Jun 4, 2012 at 6:16

1 Answer 1

1

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:

- (IBAction)showMissed:(id)sender {    
  NSMutableIndexSet *indices = [NSMutableIndexSet indexSet];
  Call *call;

  [self.recentsTableView reloadData];

  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];
  }
}
1
  • I don't know why but this solves the problem of getting an EXC_BAD_ACCESS when using removeRowsAtIndexes animated at the same time of a removeObjectsAtIndexes call on the datasource array. Thanks for sharing, maybe someone can explain why works like this. Apr 19, 2013 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.