Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSRangeException in a UITableViewController with mixed static/dynamic content

Using iOS6 and storyboards, I have a UITableViewController with 3 sections.

The first 2 sections are static, and the third is dynamic.

I have overridden numberOfRowsInSection, cellForRowAtIndexPath and heightForRowAtIndexPath to properly defer to super for sections 0 and 1, and to return appropriate values from my data model for section 2.

Still, I get a NSRangeException in main() when the number of rows in my data exceeds the number of rows configured in the Storyboard for the third section:

'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** First throw call stack: (0x22f3012 0x178ae7e 0x22a8b44 0xb0ece4 0x8feee3 0x784f13 0x71940c 0x784a7b 0x789919 0x7899cf 0x7721bb 0x782b4b 0x71f2dd 0x179e6b0 0x558fc0 0x54d33c 0x54d150 0x4cb0bc 0x4cc227 0x4cc8e2 0x22bbafe 0x22bba3d 0x22997c2 0x2298f44 0x2298e1b 0x26867e3 0x2686668 0x6ceffc 0x2c4d 0x2b75) libc++abi.dylib: terminate called throwing an exception

Which superclass methods must I override in order to prevent this exception from occurring?

like image 918
Glenn Barnett Avatar asked Apr 17 '13 14:04

Glenn Barnett


1 Answers

The implementation I was missing was:

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { return 0; }

This means there are a total of four methods one must override to inject dynamic behavior into a static prototype UITableViewController:

  1. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

  2. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  3. -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

  4. -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

Hope this saves someone some time.

like image 143
Glenn Barnett Avatar answered Sep 23 '22 17:09

Glenn Barnett