PrEV
Thoughts from a NeXTStep Guy on Cocoa Development

Quick Table View Weirdness

May 14, 2009 by Bill Dudney

While working on a chapter of the iPhone book I came across an interesting feature/bug in table view.

Typically while adding content to a table view you don't want to call reloadData unless you absolutely have to because that pitches the whole stack of work that the table view did to get its drawing done. Instead you call one of insertRowsAtIndexPaths:withRowAnimation: or deleteRowsAtIndexPaths:withRowAnimation:. All this works fine if you start with at least one section. The example I was writing started with zero sections.

Adding a new object then calling insertRowsAtIndexPaths:withRowAnimation: leads to a really ugly error message. Like this;

2009-05-14 14:11:41.131 Conference[2716:20b] Serious application error.  
Exception was caught during ... change processing: 
*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0) with userInfo (null)

[Session started at 2009-05-14 14:11:41 -0600.]
2009-05-14 14:11:41.132 Conference[2716:20b] 
*** Terminating app due to uncaught exception 'NSRangeException', reason: 
'*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

If on the other hand you call insertSections:withRowAnimations: for that initial insert, everything is a-ok.

Hope this helps someone running into the same issue.

Permalink     7 Comments - Add Yours