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

后端 未结 6 720
盖世英雄少女心
盖世英雄少女心 2020-12-04 12:53

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 scrollToRowAtI

6条回答
  •  执念已碎
    2020-12-04 12:54

    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.

提交回复
热议问题