有人能教我如何使用prepareForReuse吗?我已经搜索了几个小时,阅读了开发人员的文档。
在我的自定义单元格中,扩展了UITableViewCell,我有prepareForReuse方法并调用了它,但我该如何处理它(有呈现问题)。我是否要为每个标签设置deadline = @"“?
@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])) {
// Initialization 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发布于 2011-03-02 11:37:07
一旦对象被构造,调用任何init方法都是不可接受的,因此必须有某种方法在对象被重用之前将其重置回中立状态。这就是prepareForReuse的作用。您可以使用该方法将对象放回调用init方法后的相同状态,以便调用代码将执行相同的操作,无论它是被赋予新对象还是被重用的对象。
发布于 2013-05-26 22:09:18
在我的一个应用程序中,我有一个持久化对象实例,每行一个,它们知道“他们的”UITableViewCell实例。(当行是可见的,所以他们实际上有一个)。
在这种情况下,prepareForReuse只是一个机会,它可以告诉一个行对象,它的UITableViewCell实例即将被提供给其他某个行对象,即原始行在表视图中不再可见。
发布于 2013-07-05 10:37:03
(一个迟来的答案--但对于那些偶然发现这个问题的人来说)
如果您不想子类化UITableViewCell并覆盖-(void)prepareForReuse方法,可以考虑使用<UITableViewDelegate>方法:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// prepare cell before it is displayed
}这将允许您在显示单元格之前对其执行prepare。
https://stackoverflow.com/questions/5162700
复制相似问题