Search code examples
cocoacore-animationnstableviewgrand-central-dispatch

View-based NSTableView renders blank rows after inserting new rows with animation


I have a view-based NSTableView that I'm backing with an `NSMutableArray . Periodically I go out grab some data and want to insert new rows into the table at the top.

When I do this without specifying an animation to insertRowsAtIndexes:withAnimation: it seems to work fine for hours on end. However, if I specify an animation, after about 7 or 8 inserts the table view starts to show blank rows where the inserts occurred. Scrolling down and then back up causes the new rows to render properly.

The code that calls insertRowsAtIndexes:withAnimation is in a block, and not running on the main thread, but my inserts happen inside of dispatch_async on the main queue, so I dont' think it related to multithreading.

Here is some code ... self.contents is my NSMutableArray.

 dispatch_async(dispatch_get_main_queue(), ^{
                [self.contentsTableView beginUpdates];
                if(posts.count) {
                    for(NSDictionary* dictionary in [posts reverseObjectEnumerator]) {
                        FNPost* post = [[FNPost alloc] initWithDictionary:dictionary width:self.contentsTableView.frame.size.width];
                        post.delegate = self;
                        [self.contents insertObject:post atIndex:0];
                        [self.contentsTableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:0] withAnimation: NSTableViewAnimationSlideLeft];
                    }
                }
                [self.contentsTableView endUpdates];

            });

One thing I'm confused about is the part of the Apple NSTableView documentation for insertRowsAtIndexes:withAnimation that says:

The numberOfRows in the table view will automatically be decreased by the count of indexes.

I'm confused by what that statement implies about the relationship between the number of objects in my array (and hence the result of numberOfRowsInTableView:) and the number of rows the table view thinks it has. To my thinking the number of rows in the table view should equal my array count and I want to make sure that my understanding of this is not at the root of the problem but like I said, the table works fine if no animation is specified.

Am I doing something obviously wrong here?


Solution

  • This issue turns out to be caused by having the NSTableView in a layer based view hierarchy. Two superviews up, I had a layer-backed view. After reading some other posts about NSTableView showing other errant behavior when in a layer-backed hierarchy I decided to turn off the layer in IB. Table animations now occur as I would expect at the expense of having to maintain some additional state to control the layer backing state when I want to use CA transitions between my views.