How to programmatically have UITableView scroll to a specific section

前端 未结 6 1439
离开以前
离开以前 2021-02-13 20:40

In my calendar app, I want my UITableView to scroll to a specific section based on which date is tapped. Current implementation is below:

- (void)calendarDidDate         


        
相关标签:
6条回答
  • Swift version of @black-sheep answer:

    let sectionIndexPath = IndexPath(row: NSNotFound, section: section)
    
    0 讨论(0)
  • 2021-02-13 21:07

    Swift 3.0

    DispatchQueue.main.async {
       let indexPath = IndexPath(item: 'item', section: 'section')
       self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
    }
    
    0 讨论(0)
  • 2021-02-13 21:18

    You have to find/calculate the section and row that you want to scroll

    NSInteger row = ??;//you have to find/calculate which row you want to show
    NSInteger section = ??;//you have to find/calculate which row of the sections you want to show
    NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
    
    [self.agendaTable scrollToRowAtIndexPath:ip
                            atScrollPosition:UITableViewScrollPositionNone
                                    animated:YES];
    
    0 讨论(0)
  • 2021-02-13 21:23

    When your section has no cells, for example it has just a header or footer, you can use one of the following options:

    let sectionRect = yourTable.rect(forSection: sectionNumber)
    yourTable.setContentOffset(CGPoint(x: sectionRect.origin.x, y: sectionRect.origin.y), animated: true)
    

    or

    let sectionRect = yourTable.rect(forSection: sectionNumber)
    yourTable.scrollRectToVisible(sectionRect, animated: false)
    
    0 讨论(0)
  • 2021-02-13 21:23

    Row is must be int value and that variable define in .h and use this variable in anywhere in .m class that you are want to scroll on particular position.

    You can scroll UITableView on particular section and also you can scroll to section's particular index cell using following code:

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [MytblView scrollToRowAtIndexPath:indexPath 
                         atScrollPosition:UITableViewScrollPositionTop 
                                 animated:YES];
    
    0 讨论(0)
  • 2021-02-13 21:28

    Try this

    [NSIndexPath indexPathForRow:NSNotFound inSection:section]
    
    0 讨论(0)
提交回复
热议问题