【问题标题】:How to detect when a custom tableview cell goes off the screen?如何检测自定义表格视图单元格何时离开屏幕?
【发布时间】:2017-06-08 11:34:12
【问题描述】:

在我的应用中,我有一个自定义 tableViewCell,

我还有一个带有 tableView 的 View Controller,我在其中显示自定义 tableViewCells。

是否有一种方法(ViewController 方法或 TableViewCell 方法)在 tableViewCell 离开屏幕时触发,我需要在其中为我的单元格进行一些核心数据更新。

(我需要知道方法,因为当它离开屏幕时,我需要在我的 tableViewCell 中更新 Timer() 的 Core Data 值。)

【问题讨论】:

  • 为什么不只在可见单元格中更新?
  • Core Data 与表格视图的视觉状态紧密耦合似乎是一个有缺陷的架构。

标签: ios swift uitableview tableview cell


【解决方案1】:

以下 tableview 委托用于检测单元格结束显示 :: tableView:didEndDisplayingCell:forRowAtIndexPath:

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath)

当单元格从表格视图中移除或单元格结束其显示时触发上述方法。

【讨论】:

    【解决方案2】:

    你可以使用 UITableView 的tableView:didEndDisplayingCell:forRowAtIndexPath:deleaget 方法

    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
        }
    

    这里请注意,当你重新加载tableview或cell时,正在被移除的cell会触发这个方法。所以在这个方法中实现额外的逻辑来处理这种情况。

    【讨论】:

      【解决方案3】:

      有一个tableView(_:didEndDisplaying:forRowAt:) 委托方法: https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614870-tableview

      【讨论】:

      • Use this method to detect when a cell is removed from a table view, as opposed to monitoring the view itself to see when it appears or disappears. 看起来这可能会做不同的事情。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 2010-10-24
      • 2015-12-25
      • 1970-01-01
      相关资源
      最近更新 更多