检测 UITableView 部分何时滚出视图

Man*_*nav 2 uitableview ios swift

我试图在第一部分滚动到视图之外时删除它。

我使用下面的委托方法尝试了它,但由于我没有页脚视图,所以没有帮助。

func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int)
Run Code Online (Sandbox Code Playgroud)

另外,我尝试使用滚动视图委托

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){
  let indexPath = tableView.indexPathsForVisibleRows?.first
  if indexPath?.section == 1 {
       //remove View
  }
}
Run Code Online (Sandbox Code Playgroud)

你能告诉我如何检测第一部分何时消失?

Moj*_*ini 6

您可以在委托中使用...didEndDisplayingCell...函数:

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.section == 0 && indexPath.row == lastRowInFirstSection {
        // first section is out
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,一旦每个单元格从屏幕顶部或底部出来,就会调用此函数,因此您需要检查indexPath以确保这是您需要的单元格。

如果需要,您还可以检查第二部分是否可见,以检测第一部分是否从底部伸出:

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.section == 0 && indexPath.row == lastRowInFirstSection {

        if tableView.indexPathsForVisibleRows?.contains (where: { $0.section == 0 }) == true {
            // It goes out from bottom. So we have to check for the first cell if needed
        } else {
            // It goes out from top. So entire section is out.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)