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

后端 未结 6 699
盖世英雄少女心
盖世英雄少女心 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.

    0 讨论(0)
  • 2020-12-04 13:04

    I think a blank row is probably the only way to go there. Is it possible to redesign the UI such that the "empty" row can display something useful?

    I say try it out, and see what the performance is like. They give pretty dire warnings about using transparent sub-views in your list items, and I didn't find that it mattered all that much in my application.

    0 讨论(0)
  • 2020-12-04 13:09

    This is an old question, but Apple still haven't added anything which helps or fixed the crash bug where the section has no rows.

    For me, I really needed to make a new section scroll to the middle when added, so I now use this code:

    if (rowCount > 0) {
        [self.tableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: sectionIndexForNewFolder] 
                              atScrollPosition: UITableViewScrollPositionMiddle
                                      animated: TRUE];
    } else { 
        CGRect sectionRect = [self.tableView rectForSection: sectionIndexForNewFolder];
        // Try to get a full-height rect which is centred on the sectionRect
        // This produces a very similar effect to UITableViewScrollPositionMiddle.
        CGFloat extraHeightToAdd = sectionRect.size.height - self.tableView.frame.size.height;
        sectionRect.origin.y -= extraHeightToAdd * 0.5f;
        sectionRect.size.height += extraHeightToAdd;
        [self.tableView scrollRectToVisible:sectionRect animated:YES];
    }
    

    Hope you like it - it's based on Mike Akers' code as you can see, but does the calculation for scrolling to the middle instead of top. Thanks Mike - you're a star.

    0 讨论(0)
  • 2020-12-04 13:09

    Since using:

    [NSIndexPath indexPathForRow:NSNotFound inSection:EXAMPLE]
    

    Broken for me in Xcode 11.3.1 (iOS simulator - 13.3) I decided to use:

    NSUInteger index = [self.sectionTypes indexOfObject:@(EXAMPLE)];
    if (index != NSNotFound) {
        CGRect rect = [self.tableView rectForSection:index];
        [self.tableView scrollRectToVisible:rect animated:YES];
    }
    
    0 讨论(0)
  • 2020-12-04 13:18

    If your section have not rows use this

    let indexPath = IndexPath(row: NSNotFound, section: section)
    tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
    
    0 讨论(0)
  • 2020-12-04 13:18

    A Swift approach to the same:

    if rows > 0 {
        let indexPath = IndexPath(row: 0, section: section)
        self.tableView.setContentOffset(CGPoint.zero, animated: true)
        self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
    }
    
    else {
        let sectionRect : CGRect = tableView.rect(forSection: section)
        tableView.scrollRectToVisible(sectionRect, animated: true)
    }
    
    0 讨论(0)
提交回复
热议问题