ios 检测 UITableView 何时滚动到底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39015228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect when UITableView has scrolled to the bottom
提问by Joseph Beuys' Mum
Is the following post still the accepted way of detecting when an instance of UITableView has scrolled to the bottom [in Swift], or has it been altered (as in: improved) since?
以下帖子是否仍然是检测 UITableView 实例何时滚动到底部 [在 Swift 中] 的公认方式,或者它是否已被更改(如:改进)?
Problem detecting if UITableView has scrolled to the bottom
Thank you.
谢谢你。
回答by Anbu.Karthik
try this
尝试这个
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let height = scrollView.frame.size.height
let contentYoffset = scrollView.contentOffset.y
let distanceFromBottom = scrollView.contentSize.height - contentYoffset
if distanceFromBottom < height {
print(" you reached end of the table")
}
}
or you can find in this way
或者你可以通过这种方式找到
if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) {
//you reached end of the table
}
回答by Giang
Swift 3
斯威夫特 3
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row + 1 == yourArray.count {
print("do something")
}
}
回答by JPetric
We can avoid using scrollViewDidScroll
and use tableView:willDisplayCell
我们可以避免使用scrollViewDidScroll
和使用tableView:willDisplayCell
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == tableView.numberOfSections - 1 &&
indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
// Notify interested parties that end has been reached
}
}
This should work for any number of sections.
这应该适用于任意数量的部分。
回答by onmyway133
In Swift 4
在斯威夫特 4
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let isReachingEnd = scrollView.contentOffset.y >= 0
&& scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)
}
If you implement expandable UITableView/UICollectionView
, you may need to check scrollView.contentSize.height >= scrollView.frame.size.height
如果你实现了 expandable UITableView/UICollectionView
,你可能需要检查scrollView.contentSize.height >= scrollView.frame.size.height