How to determine if the user has scrolled to the bottom of the UITableView?

我的未来我决定 提交于 2020-01-22 13:58:06

问题


How can I determine if the user has scrolled to the last cell/bottom of a UITableView?


回答1:


UITableView inherits from UIScrollView, and scroll view exposes a contentOffset property (documentation here).

Use this with a bit of math to determine if the contentOffset is within frame.size.height of the bottom.

Update: here's a stab at a formula that will give you what you want:

if(tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height)) {
  //user has scrolled to the bottom
}



回答2:


Use NSArray *paths = [tableView indexPathsForVisibleRows];. Then check if the last object in that array is the indexPath for the final cell.

Source: Another Question




回答3:


Thanks to newer iOS versions, there's an easy way with the willDisplayCell function:

func tableView(tableView:UITableView, willDisplayCell cell:UITableViewCell, forRowAtIndexPath indexPath:NSIndexPath) {

       if (indexPath.row >= tableView.numberOfRowsInSection(0)) {

           NSLog("User got to bottom of table")

       } 
}

Note that UICollectionViews have a similar function:

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {

}


来源:https://stackoverflow.com/questions/3160642/how-to-determine-if-the-user-has-scrolled-to-the-bottom-of-the-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!