Can someone please show me how to use prepareForReuse? I have been searching for hours and read dev docs.
In my custom cell, which extends UITableViewCell I have the prepareForReuse method and its getting called, but what do I do with it (having rendering issues). Do I do this deadline = @"" for each label?
@implementation PostTableCustomCellController
@synthesize authorName;
@synthesize deadline;
@synthesize distance;
@synthesize interestedCount;
@synthesize description;
@synthesize avatar;
@synthesize viewForBackground;
@synthesize fetchedResultsController, managedObjectContext;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Init开发者_开发问答ialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void) prepareForReuse {
NSLog(@"prep for reuse");
[self clearFields];
}
- (void) clearFields {
NSLog(@"clearFields was called Jason");
}
- (void)dealloc {
[super dealloc];
}
@end
Once an object is constructed, calling the any of the init
methods is unacceptable, so there must be some way to reset the object back to a neutral state before it gets reused. That's what prepareForReuse
is for. You use that method to put the object back in the same state it was in right after the init
method was called so that the calling code will do the same thing, whether it is given a new object or a reused one.
In one of my apps I have persistent object instances, one per row, which know about 'their' UITableViewCell instance. (When the row is visible so they actually have one).
In this case, prepareForReuse
is just an opportunity to tell a row object that its UITableViewCell instance is about to be given away to some other row object, i.e. the original row no longer has a visible presence in the table view.
(A late answer - but for those who stumble across this question)
If you don't want to subclass UITableViewCell and override the -(void)prepareForReuse
method, you could consider using the <UITableViewDelegate>
method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// prepare cell before it is displayed
}
This would allow you to prepare
the cell prior to it being displayed.
精彩评论