我想检测UITableView
节标题何时贴紧在屏幕顶部,然后修改标题的高度,但是我不知道该怎么做。有人可以帮忙吗?
通过使用didEndDisplayingHeaderView
和willDisplayHeaderView
委托方法,我能够完成检测哪个标头卡在表格视图顶部的功能。这里的想法是,当我们滚动浏览表视图的各个部分时,标题周围的行变得可见,并且可以使用捕获所有可见行的索引路径tableView.indexPathsForVisibleRows
。根据我们是向上滚动还是向下滚动,我们将屏幕上的第一个或最后一个indexPath与刚刚出现/消失的视图的索引路径进行比较。
//pushing up func tableView(_ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) { //lets ensure there are visible rows. Safety first! guard let pathsForVisibleRows = tableView.indexPathsForVisibleRows, let lastPath = pathsForVisibleRows.last else { return } //compare the section for the header that just disappeared to the section //for the bottom-most cell in the table view if lastPath.section >= section { print("the next header is stuck to the top") } } //pulling down func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { //lets ensure there are visible rows. Safety first! guard let pathsForVisibleRows = tableView.indexPathsForVisibleRows, let firstPath = pathsForVisibleRows.first else { return } //compare the section for the header that just appeared to the section //for the top-most cell in the table view if firstPath.section == section { print("the previous header is stuck to the top") } }