22

如何检测 a 何时UITableView滚动到底部以便最后一个单元格可见?

4

8 回答 8

41

里面tableView:cellForRowAtIndexPath:还是tableView:willDisplayCell:forRowAtIndexPath:这样的:

- (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
    }

    ...

}
于 2010-07-13T20:55:54.063 回答
26

在你的 UITableViewDelegate 中实现该tableView:willDisplayCell:forRowAtIndexPath:方法并检查它是否是最后一行。

于 2010-07-13T20:52:35.610 回答
14
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
    NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
    if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
        // This is the last cell
    }
}
于 2014-03-29T13:58:20.163 回答
6

为:创建优雅的扩展UITableView

extension UITableView {

    func isLast(for indexPath: IndexPath) -> Bool {

        let indexOfLastSection = numberOfSections > 0 ? numberOfSections - 1 : 0
        let indexOfLastRowInLastSection = numberOfRows(inSection: indexOfLastSection) - 1

        return indexPath.section == indexOfLastSection && indexPath.row == indexOfLastRowInLastSection
    }
}

使用示例:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if tableView.isLast(for: indexPath) {
        //do anything then
    }

    return cell
}
于 2017-05-10T10:21:07.203 回答
4

斯威夫特 5+

//Show Last Cell (for Table View)
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
{
    if indexPath.row == (yourdataArray.count - 1) 
    {
        print("came to last row")
    }
}

//Show last cell (for Collection View)
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) 
{
    if indexPath.row == (yourdataArray.count - 1)
    {
        // Last cell is visible
    }
}
于 2017-02-28T06:06:44.510 回答
2

对于 Xcode 8 和 swift 3

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let lastSectionIndex = tableView.numberOfSections - 1
        let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
        if indexPath.section ==  lastSectionIndex && indexPath.row == lastRowIndex {
            print("this is the last cell")
        }
    }
于 2017-02-22T07:48:22.057 回答
1

我找到了这个解决方案:

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()
      }
}
于 2020-11-06T11:33:04.083 回答
0

willDisplayCell 方法有一些问题。这对我有用:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let y = scrollView.contentOffset.y/(scrollView.contentSize.height - scrollView.frame.size.height)
    let relativeHeight = 1 - (table.rowHeight / (scrollView.contentSize.height - scrollView.frame.size.height))
    if y >= relativeHeight{
        print("last cell is visible")
    }
}

只需在委托方法中传递它,并将“表”更改为您的 tableView。

于 2017-10-26T00:44:56.807 回答