2016-01-23 94 views
-1

当用户向下滚动tableView时,单元正在出列并排队。我的UITableViewCell如何知道它何时会被显示和销毁?

我想知道,在我的UITableViewCell里发生这种情况。

如果这是不可能的,我们可以用通知中心来实现这一(使用表格视图的代表?)

注:我想要的细胞本身知道什么时候它被出队。 我知道已经有2个UITableView的代表可以使用,但我宁愿不使用它们。

+5

这与您以前的问题有何不同? - http://stackoverflow.com/questions/34957992/in-uitableview-whats-the-delegate-for-visiblecells? – Paulw11

+1

@ Paulw11这个问题在你所关联的问题中确实有了答案。 TIMEX,请让我们知道如果不是这种情况,否则问题可能会被重复关闭。 – Cristik

回答

1

我有类似的任务,我来用这个方法:

override func willMoveToSuperview(_ newSuperview: UIView?) 
{ 
    super.willMoveToSuperview(newSuperview) 
    if newSuperview != nil 
    { 
     // Cell will be added to collection view 
    } 
    else 
    { 
     // Cell will be removed 
    } 
} 

override func didMoveToSuperview() 
{ 
    // You can also override this method, check self.superview 
} 

我记得这些方法的工作更加稳定比prepareForReuse()。但是委托方法无论如何都更加健壮。

+0

在很多情况下,不幸的是,这不起作用 - 它已经在超级视图中,并且表视图系统将其滑入屏幕中... – Fattie

0

当一个单元格不再需要时,它将从UITableView中删除,为了检测何时发生这种情况,您可以覆盖removeFromSuperView方法。但是当你滚动时,单元格会被重用,所以你还需要在prepareForReuse中做同样的清理。

至于检测它何时被添加到表格视图中,您可以添加一个configure方法,该方法被cellForRowAtIndexPath:调用,因为很可能您需要实施cellForRowAtIndexPath:

class MyTableViewCell: UITableViewCell { 

    func configure(obj: MyDataObject) { 
     // intialize whathever you need 
    } 

    func doCleanup() { 
     // cleanup the stuff you need 
    } 

    override func removeFromSuperview() { 
     super.removeFromSuperview() 
     doCleanup() 
    } 

    override func prepareForReuse() { 
     super.prepareForReuse() 
     doCleanup() 
    } 
} 
相关问题