swift 确定某一行是否从视图中消失

swift determine if a certain row disappear from view

我一直在四处寻找解决方案,但直到现在都没有结果。我想要做的是确定某个单元格是否从屏幕上消失了。

这是我一直在尝试的代码:

func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    let indexPathWatchCell = NSIndexPath(forRow: 4, inSection: 0)

    if ((tableView.indexPathsForVisibleRows?.contains(indexPathWatchCell)) == nil){
        NSLog("it disappeared!!")
    }
}

不幸的是它不起作用,有什么想法吗?

编辑:

为清楚起见,我想要完成的是当单元格在视图中消失时将其大小设置为 CGFLOAT(44)

contains 方法 return 布尔值,如果对象包含其他明智的 return false,所以你需要检查那个而不是 nil 所以改变你的代码这个。

if (tableView.indexPathsForVisibleRows?.contains(indexPathWatchCell)){
    NSLog("it not disappeared!!")
}
else {
    NSLog("it's disappeared!!")
}    

或者,如果您只想知道 "disappeared",您可以将 !(不是)与 if

一起使用
if (!tableView.indexPathsForVisibleRows?.contains(indexPathWatchCell)){
    NSLog("it disappeared!!")
}