【发布时间】:2016-08-18 09:59:00
【问题描述】:
以下帖子是否仍然是检测 UITableView 实例何时滚动到底部 [在 Swift 中] 的公认方法,或者它是否已被更改(如:改进)?
Problem detecting if UITableView has scrolled to the bottom
谢谢。
【问题讨论】:
标签: ios swift uitableview
以下帖子是否仍然是检测 UITableView 实例何时滚动到底部 [在 Swift 中] 的公认方法,或者它是否已被更改(如:改进)?
Problem detecting if UITableView has scrolled to the bottom
谢谢。
【问题讨论】:
标签: ios swift uitableview
试试这个
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")
}
}
或者你可以这样找到
if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) {
//you reached end of the table
}
【讨论】:
if distanceFromBottom <= height 才能使其正常工作。
斯威夫特 3
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row + 1 == yourArray.count {
print("do something")
}
}
【讨论】:
indexPath。您可以轻松确定表格视图的结束位置
if indexPath.section == COUNT_OF_SECTIONS - 1 { if indexPath.row + 1 == LAST_DATA_SET.count { print("do something")
我们可以避免使用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
}
}
这应该适用于任意数量的部分。
【讨论】:
在 Swift 4 中
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let isReachingEnd = scrollView.contentOffset.y >= 0
&& scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)
}
如果你实现了可扩展的UITableView/UICollectionView,你可能需要检查scrollView.contentSize.height >= scrollView.frame.size.height
【讨论】: