UITableView Detecting Last Cell

前端 未结 8 741
广开言路
广开言路 2021-01-31 10:46

How can I detect when a UITableView has been scrolled to the bottom so that the last cell is visible?

相关标签:
8条回答
  • 2021-01-31 11:42

    Inside tableView:cellForRowAtIndexPath: or tableView:willDisplayCell:forRowAtIndexPath: like this:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        ...
    
        NSInteger sectionsAmount = [tableView numberOfSections];
        NSInteger rowsAmount = [tableView numberOfRowsInSection:[indexPath section]];
        if ([indexPath section] == sectionsAmount - 1 && [indexPath row] == rowsAmount - 1) {
            // This is the last cell in the table
        }
    
        ...
    
    }
    
    0 讨论(0)
  • 2021-01-31 11:48

    I found this solution:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
          let contentOffsetMaxY: Float = Float(scrollView.contentOffset.y + scrollView.bounds.size.height)
          let contentHeight: Float = Float(scrollView.contentSize.height)
          let lastCellIsVisible = contentOffsetMaxY > contentHeight + somePaddingIfWanted
          if lastCellIsVisible {
               doSomething()
          }
    }
    
    0 讨论(0)
提交回复
热议问题