UITableView can't be scrolled to a section containing 0 rows

Originator:mike.akers
Number:rdar://6263339 Date Originated:
Status:Closed Resolved:
Product: Product Version:
Classification: Reproducible:
 
01-Oct-2008 07:23 PM Michael Akers:
Summary: 
In an app I'm working on, I have a sectioned UITableView that can contain a section containing zero rows. I want to be able to scroll to this section using scrollToRowAtIndexPath:atScrollPosition:animated: but I get the following error when I try to scroll to this section due to the lack of child rows:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: row (2147483647) beyond bounds (0) for section (10).

Steps to Reproduce:
Please see the attached XCode project which demonstrates this bug..

Create a sectioned UITableView containing an empty section and try to scroll to it using scrollToRowAtIndexPath:atScrollPosition:animated:

Expected Results:
UITable view should scroll to the requested section

Actual Results:
The following NSRangeException is thrown:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]: row (2147483647) beyond bounds (0) for section (10).

Regression:
NA

Comments

Looks like this bug is fixed in 3.0. You can use the following NSIndexPath to scroll to a section containing 0 rows:

[NSIndexPath indexPathForRow:NSNotFound inSection:section]

By mike.akers at July 14, 2009, 1:16 a.m. (reply...)

Found a decent workaround:

CGRect sectionRect = [self.tableView rectForSection:indexOfSectionToScrollTo];
[tableView scrollRectToVisible:todaysSectionRect animated:YES];

The code above will scroll the tableview so the desired section is visible but not necessarily at the top or bottom of the visible area. If you want to scroll so the section is at the top do this:

CGRect sectionRect = [self.tableView rectForSection:indexOfSectionToScrollTo];
sectionRect.size.height = self.tableView.frame.size.height;
[tableView scrollRectToVisible:todaysSectionRect animated:YES];

Modify sectionRect as desired to scroll the desired section to the bottom or middle of the visible area.

By mike.akers at Dec. 8, 2008, 10:11 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!