Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I scroll a UITableView to a section that contains no rows?

In an app I'm working on, I have a plain style 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 an error when I try to scroll to this section due to the lack of child rows.

Apple's calendar application is able to do this, if you look at your calendar in list view, and there are no events in your calendar for today, an empty section is inserted for today and you can scroll to it using the Today button in the toolbar at the bottom of the screen. As far as I can tell Apple may be using a customized UITableView, or they're using a private API...

The only workaround I can think of is to insert an empty UITableCell in that's 0 pixels high and scroll to that. But it's my understanding that having cells of varying heights is really bad for scrolling performance. Still I'll try it anyway, maybe the performance hit won't be too bad.

Update

Since there seems to be no solution to this, I've filed a bug report with apple. If this affects you too, file a duplicate of rdar://problem/6263339 (Open Radar link) if you want this to get this fixed faster.

Update #2

I have a decent workaround to this issue, take a look at my answer below.

like image 792
Mike Akers Avatar asked Oct 01 '08 21:10

Mike Akers


People also ask

Is UITableView scrollable?

UITableView scrolls back because it's content size is equal to it's frame (or near to it). If you want to scroll it without returning you need add more cells: table view content size will be large then it's frame.

How do I scroll to the top of a Tableview table?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.

What is Section in indexPath?

The section and the row of an index path is a special case (actually an extension to NSIndexPath for UIKit, see this post). For a given NSIndexPath indexPath [indexPath indexAtPosition:0]; is the same as [indexPath section];


2 Answers

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

[NSIndexPath indexPathForRow:NSNotFound inSection:section] 

I'll leave my original workaround here for anyone still maintaining a project using the 2.x SDK.


Found a decent workaround:

CGRect sectionRect = [tableView rectForSection:indexOfSectionToScrollTo]; [tableView scrollRectToVisible:sectionRect 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 = [tableView rectForSection:indexOfSectionToScrollTo]; sectionRect.size.height = tableView.frame.size.height; [tableView scrollRectToVisible:sectionRect animated:YES]; 

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

like image 125
Mike Akers Avatar answered Sep 21 '22 20:09

Mike Akers


If your section have not rows use this

let indexPath = IndexPath(row: NSNotFound, section: section) tableView.scrollToRow(at: indexPath, at: .middle, animated: true) 
like image 21
Vladimir Pchelyakov Avatar answered Sep 22 '22 20:09

Vladimir Pchelyakov