检测UITableViewCell何时离开屏幕 [英] Detect when UITableViewCell goes off the screen
问题描述
我正在用自定义创建的UITableViewCell实现一个丰富的UITableView,我以一种方式在屏幕上显示这些,但是一旦它们离开屏幕我想记下这一点,因为他们第二次出现我会喜欢他们以不同的方式显示。离开屏幕时想想自动标记为已读。
I'm implementing a rich UITableView with custom created UITableViewCell, I show these on the screen in one fashion, but once they go off the screen I want to take a note of that, since the second time they come on I would like them to get displayed in a different manner. Think auto "mark as read" when going off the screen.
我一直在寻找某种方法来检测一个单元格离开屏幕的时间(得到dealloc'ed)或出列或等效),最好在:UITableViewController类中快速记下[indexPath行],但在:UITableViewCell同样好。
I've been looking for some way to detect when a cell goes off the screen (get's dealloc'ed or dequeued or equivalent), preferably in the :UITableViewController class to make a quick note of the [indexPath row], but in the :UITableViewCell is equally as good.
I无法以任何标准方式执行此操作...计算它出现的时间似乎是不可能的,因为我在桌面上执行多次reloadData调用。
I haven't been able to do this in any standard way ... counting the times it appeared seems out of the question as I do multiple reloadData calls on the table.
任何想法?这看起来有点棘手:)
Anyone any ideas? This seems a bit tricky :)
推荐答案
这是一个老问题,但是如果有人在iOS6中寻找一个新的问题引入UITableViewDelegate函数就是这样:
This is an old question, but in case anyone is looking, in iOS6, a new UITableViewDelegate function was introduced that does just this:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
在删除单元格时,它可以很好地告诉您但是,它非常彻底,因此如果你做了一个重载单元,即使是被替换的旧单元也会触发这个委托功能。在我的实现中,我只是检查传递的 indexPath
是否仍在数组 tableView.indexPathsForVisibleRows
中。类似于:
It does a great job at telling you whenever a cell is removed, however, it is very thorough and thus if you did a reload cell, even the old cell that's being replaced will trigger this delegate function. In my implementation I simply check to see if the indexPath
passed is still within the array tableView.indexPathsForVisibleRows
. Something like:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView.indexPathsForVisibleRows indexOfObject:indexPath] == NSNotFound)
{
// This indeed is an indexPath no longer visible
// Do something to this non-visible cell...
}
}
这篇关于检测UITableViewCell何时离开屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!