Jos*_*Mum 28 uitableview ios swift
以下帖子仍然是检测UITableView实例何时滚动到底部[在Swift中],或者是否已被更改(如:改进)的可接受方式?
谢谢.
Anb*_*hik 60
试试这个
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")
}
}
Run Code Online (Sandbox Code Playgroud)
或者你可以这样找到
if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) {
//you reached end of the table
}
Run Code Online (Sandbox Code Playgroud)
Gia*_*ang 37
斯威夫特3
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row + 1 == yourArray.count {
print("do something")
}
}
Run Code Online (Sandbox Code Playgroud)
在斯威夫特 4
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let isReachingEnd = scrollView.contentOffset.y >= 0
&& scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)
}
Run Code Online (Sandbox Code Playgroud)
如果你实现了 expandable UITableView/UICollectionView
,你可能需要检查scrollView.contentSize.height >= scrollView.frame.size.height
我们可以避免使用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
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于任何数量的部分。
归档时间: |
|
查看次数: |
29608 次 |
最近记录: |