【问题标题】:How can I detect the bottom of tableview with lots of sections? (in swift)如何检测有很多部分的 tableview 的底部? (迅速)
【发布时间】:2019-03-03 03:21:04
【问题描述】:
我想知道当我在 swift4 中有很多部分的 tableView 单元格的底部向下滚动时如何检测。
我知道我可以使用此功能检测到它,
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {}
但它仅在有 1 个部分时才有效。
当有很多部分时,请告诉我如何检测表格视图单元格的底部。
【问题讨论】:
标签:
ios
swift
tableview
detect
sections
【解决方案1】:
您可以通过验证 tableview 的 scrollViewDidScroll 委托来发现 tableview 滚动到底部...并且验证的是数组中的最后一部分
extension YourViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if someFlag == SectionArray.count - 1
{
if scrollView.contentSize.height - scrollView.contentOffset.y - scrollView.frame.height < 100 {
// only 100 pt are left from the bottom
}
}
}
}
【解决方案2】:
根据您要检查的具体内容,您可以执行以下操作:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == tableView.numberOfSections - 1 {
print("will display last section")
if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
print("will display last row")
}
}
}
【解决方案3】:
您可以通过两种方式获取该信息。一种方法是询问您的模型并与给定的 indexPath 部分和行进行比较以检查它是否是最后一个部分/单元格。二是要求tableView委托方法从模型中获取相同的数据(因为tableView与模型是同步的)。
【解决方案4】:
您可以通过使用UIScrollViewDelegate 并检查contentOffset 来做到这一点
extension YourViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentSize.height - scrollView.contentOffset.y - scrollView.frame.height < 100 {
// only 100 pt are left from the bottom
}
}
}