UITableView и повторное использование ячеек
У меня есть UITableView
и я подкласс UITableViewCell
(назвал это CustomCell
) так что он имеет несколько ярлыков и UIImageView
,
Только определенные ячейки будут отображать изображение. Вот мой код для tableView:cellForRowAtIndexPath:
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Match *aMatch = [[appDelegate.matchByDate objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.homeLabel.text = aMatch.homeTeam;
cell.awayLabel.text = aMatch.awayTeam;
cell.timeLabel.text = aMatch.koTime;
cell.tournamentLabel.text = aMatch.tournament;
NSString *tempString = [appDelegate.teamLogos objectForKey:[aMatch homeTeam]];
if (tempString!=nil) {
cell.homeImageView.image = [UIImage imageNamed:tempString];
}
return cell;
}
Так что это только устанавливает homeImageView
когда он находит соответствующее изображение в словаре, который я настроил. Похоже, что это работает для первых нескольких ячеек, но если я прокручиваю список, я вижу, что у ячеек есть изображение, когда у него его не должно быть.
Я понимаю, что это, вероятно, из-за повторного использования клетки, но я устанавливаю homeImageView
после того, как ячейка была создана / повторно?
Вот метод init из моего класса CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Initialization code
tournamentLabel = [[UILabel alloc] init];
tournamentLabel.textAlignment = UITextAlignmentCenter;
tournamentLabel.font = [UIFont systemFontOfSize:12];
tournamentLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
tournamentLabel.textColor = [UIColor darkGrayColor];
homeLabel = [[UILabel alloc]init];
homeLabel.textAlignment = UITextAlignmentLeft;
homeLabel.font = [UIFont systemFontOfSize:16];
homeLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
awayLabel = [[UILabel alloc]init];
awayLabel.textAlignment = UITextAlignmentRight;
awayLabel.font = [UIFont systemFontOfSize:16];
awayLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
timeLabel = [[UILabel alloc]init];
timeLabel.textAlignment = UITextAlignmentCenter;
timeLabel.font = [UIFont systemFontOfSize:30];
timeLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
timeLabel.textColor = [UIColor darkGrayColor];
homeImageView = [[UIImageView alloc]init];
awayImageView = [[UIImageView alloc]init];
[self.contentView addSubview:homeLabel];
[self.contentView addSubview:awayLabel];
[self.contentView addSubview:timeLabel];
[self.contentView addSubview:tournamentLabel];
[self.contentView addSubview:homeImageView];
[self.contentView addSubview:awayImageView];
}
return self;
}
1 ответ
Вы должны очистить вид изображения, если у вас нет изображения для отображения:
...
if (tempString!=nil) {
cell.homeImageView.image = [UIImage imageNamed:tempString];
} else {
cell.homeImageView.image = nil;
}
...
По желанию вы можете реализовать prepareForReuse
:
В Swift:
override func prepareForReuse() {
cell.homeImageView.image = nil;
}