iOS-检测UITableViewCell是否被移出可见视图? [英] iOS - Detect UITableViewCell being moved out of the visible view?

查看:159
本文介绍了iOS-检测UITableViewCell是否被移出可见视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该单元格在屏幕上不再可见时,需要通知我.

As soon as the cell is no longer visible on the screen I need to be notified.

UITableView已经有一个名为 tableView:didEndDisplayingCell:forRowAtIndexPath:的委托方法,但是从未调用过该委托方法.是的,我确实为我的UITableView设置了委托.

UITableView already has a delegate method called tableView:didEndDisplayingCell:forRowAtIndexPath: but this delegate method never gets called. And yes I do have the delegate for my UITableView set.

还有其他方法可以检测到被移除的细胞吗?我需要能够保存此单元格的内容(输入),然后再将其用于其他项目.

Any other ways to detect a cell being removed? I need to be able to save the content (inputs) of this cell before it's being reused by another item.

根据文档 tableView:didEndDisplayingCell:forRowAtIndexPath:是iOS 6和更高版本的API.有没有办法在iOS 5上实现这一目标?

According to the documentation tableView:didEndDisplayingCell:forRowAtIndexPath: is iOS 6 and higher API. Is there a way to achieve this on iOS 5?

推荐答案

在低于6.0的iOS版本上,表格视图不会发送 tableView:didEndDisplayingCell:forRowAtIndexPath:消息.

On versions of iOS older than 6.0, the table view doesn't send the tableView:didEndDisplayingCell:forRowAtIndexPath: message.

如果您使用的是 UITableViewCell 的子类,则可以通过覆盖 didMoveToWindow :

If you are using a subclass of UITableViewCell, you can get the same effect on older versions of iOS by overriding didMoveToWindow:

- (void)didMoveToWindow {
    if (self.window == nil) {
        // I have been removed from the table view.
    }
}

您可能需要给单元格一个(弱或unsafe_unretained)引用,以供您返回给表视图委托,以便您可以向委托发送消息.

You may need to give your cell a (weak or unsafe_unretained) reference back to your table view delegate so you can send the delegate a message.

但是,您不能仅依赖于 didMoveToWindow 的所有版本的iOS.在iOS 6之前,表视图始终在重用之前将表视图单元格作为子视图删除,因此该单元格在重用之前始终会收到 didMoveToWindow .但是,从iOS 6开始,表格视图可以重用单元格,而无需将其删除为子视图.表格视图将仅更改单元格的框架以将其移动到新位置.这意味着从iOS 6开始,单元在重用之前不会总是收到 didMoveToWindow .

However, you can't rely only on didMoveToWindow for all versions of iOS. Before iOS 6, a table view always removed a table view cell as a subview before reusing it, so the cell would always receive didMoveToWindow before being reused. However, starting in iOS 6, a table view can reuse a cell without removing it as a subview. The table view will simply change the cell's frame to move it to its new location. This means that starting in iOS 6, a cell does not always receive didMoveToWindow before being reused.

因此,您应该在单元格子类中实现 didMoveToWindow ,并在委托中实现 tableView:didEndDisplayingCell:forRowAtIndexPath:,并确保如果两者都被调用,则可以实现,或者如果只叫一个.

So you should implement both didMoveToWindow in your cell subclass, and tableView:didEndDisplayingCell:forRowAtIndexPath: in your delegate, and make sure it works if both are called, or if just one is called.

这篇关于iOS-检测UITableViewCell是否被移出可见视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆