ios - 检测 UITableView 部分何时滚动出 View

标签 ios swift uitableview

当第一部分滚动到视野之外时,我正在尝试删除它。

我使用下面的委托(delegate)方法进行了尝试,但由于我没有页脚 View ,所以没有帮助。

func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int)

另外,我尝试使用 ScrollView 委托(delegate)
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){
  let indexPath = tableView.indexPathsForVisibleRows?.first
  if indexPath?.section == 1 {
       //remove View
  }
}

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

最佳答案

您可以使用 ...didEndDisplayingCell...委托(delegate)中的功能:

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

请注意,一旦每个单元格从屏幕顶部或底部移出,就会调用此函数,因此您需要检查 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.
        }
    }
}

关于ios - 检测 UITableView 部分何时滚动出 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59011367/

相关文章:

ios - Swift 中的动态标签不滚动

iOS8 自动 tableview 高度在第一个单元格上错误

iphone - iPhone 中特定于设备的资源

iphone - FastPDFKit 获取文档大小

快速核心数据: save images from camera

swift - 等待完成再执行下一步

ios - UITableViewAutomaticDimension 在 iOS 9 中无法正常工作

ios - 选择 Cell 时将 UITableViewCell 置于顶部

objective-c - iOS 中的 native 应用程序可以使用哪些 Web 应用程序不能使用的功能/技术?

ios - 使用 NSString 作为键和值创建 JSON 格式