Saturday, June 8, 2013

UITableViewCell prepareForReuse example in Objective C (iOS).


UITableViewCell prepareForReuse

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

- (void)prepareForReuse

Discussion of [UITableViewCell prepareForReuse]
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.

UITableViewCell prepareForReuse example.
You need to prepare the cell for reuse. Try adding this to the EditableCellStyle2 implementation:

- (void)prepareForReuse {
    [super prepareForReuse];
    [self didTransitionToState:UITableViewCellStateDefaultMask];
}

Example of [UITableViewCell prepareForReuse].
And then in the UITableViewCell class when you override prepareForReuse:

- (void)prepareForReuse {
    [super prepareForReuse];
    self.myCustomImageView.imageURL = nil;
    self.myCustomImageView.image = nil;
    //do the rest of your prepareForReuse here:
    ...
}

End of UITableViewCell prepareForReuse example article.