Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS callback when uitableviewcell gets destroyed

As a user scrolls up and down in an uitableview cells get destroyed and created.

Is there a way to detect when a cell is going to be or has been destroyed?

like image 973
MB. Avatar asked Jul 20 '26 17:07

MB.


2 Answers

Assuming that by "getting destroyed" you actually are referring to a cell getting reused, simply implement prepareForReuse within your UITableViewCell derived class.

prepareForReuse

Prepares a reusable cell for reuse by the table view's delegate.

- (void)prepareForReuse

Discussion

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.

Availability Available in iOS 2.0 and later. See Also – initWithFrame:reuseIdentifier: @property reuseIdentifier Declared In UITableViewCell.h

like image 154
Till Avatar answered Jul 24 '26 23:07

Till


Without going into the implications of suitability or performance, another option might be to periodically check what cells remain visible, using the visibleCells method of the UITableView class:

- (NSArray *)visibleCells

As per the documentation:

Returns an array containing UITableViewCell objects, each representing a visible cell in the receiving table view.

like image 20
Thomas Fruin Avatar answered Jul 24 '26 23:07

Thomas Fruin