MB.
MB.

Reputation: 4221

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?

Upvotes: 2

Views: 4211

Answers (5)

Thomas Fruin
Thomas Fruin

Reputation: 41

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.

Upvotes: 4

Michael Ochs
Michael Ochs

Reputation: 2870

As stated above, cells aren't destroyed when the leave the screen. However there are some things you can do, to track related actions, depending on what you are trying to do.

First there is a delegate message:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

This is called before a cell enters the screen. Another possibility is the already stated prepareForReuse method of a cell.

Another approach would be: Try and override willMoveToSuperview: or any other of the related methods. I am not sure if this is fired after the cell becomes invisible, but it might work.

Best regards, Michael

Upvotes: 0

clearscreen
clearscreen

Reputation: 2027

What you're attempting to intercept is part of the internal implementation of UITableView and how it manages its cells. While there are ways in which you can attempt to intercept such behavior, I would suggest that you avoid using them, as there is no guarantee that future implementations of UITableView will maintain this behavior.

It would be better in cases such as this to consider a different approach: be it design and implement your own table class, or change your code logic.

Upvotes: 0

Till
Till

Reputation: 27597

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

Upvotes: 12

A-Live
A-Live

Reputation: 8944

You can subclass UITableViewCell and override it's dealloc method.

Any good reason to do it assuming you are reusing the cells to save the resources ?

Upvotes: 0

Related Questions