如何解决滚动TableView时如何获取节号
我想基于tableview的部分标题更改屏幕的导航标题,因此如何确定tableview中正在滚动的部分。
解决方法
您可以尝试像这样实现它:
func tableView(_ tableView: UITableView,willDisplay cell: UITableViewCell,forRowAt indexPath: IndexPath) {
let position = cell.convert(CGPoint.zero,to: tableView)
if let indexPath = tableView.indexPathForRow(at: position) {
let section = indexPath.section
print("The section is \(section)")
}
}
,
tableView(_ tableView: UITableView,willDisplay...
不会解决问题,因为可能会在下一部分出现在屏幕上之前调用它。
我建议与UIScrollViewDelegate
一起使用tableView.indexPathsForVisibleRows
方法
extension MyViewController: UITableViewDelegate,UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let visibleIndices = tableView.indexPathsForVisibleRows ?? []
let lowestVisibleSection = visibleIndices.map({$0.section}).max() ?? 0
debugPrint("I see \(lowestVisibleSection)")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。