J'ai un UITableView
et j'ai sous-classé UITableViewCell
(appelé CustomCell
) donc il a plusieurs étiquettes et UIImageView
.UITableView et Cell Reuse
Seules certaines cellules afficheront réellement une image. Voici mon code pour 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;
}
Il définit seulement le homeImageView
quand il trouve une image correspondante dans un dictionnaire, je l'ai mis en place. Cela semble fonctionner pour les premières cellules, mais si je fais défiler la liste, je trouve que les cellules ont une image alors qu'elles ne devraient pas en avoir. Je comprends que c'est probablement parce que la cellule est réutilisée, mais je règle homeImageView
après que la cellule ait été créée/réutilisée ?!
Voici la méthode init de ma classe 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;
}
Merci qui a travaillé un régal! Bien que je ne comprends toujours pas pourquoi je dois le faire quand mon CustomCell n'a pas de homeImageView mis à init? –
En raison de la réutilisation des cellules. 'dequeueReusableCellWithIdentifier' vous renverra une cellule qui a déjà été utilisée. Si elle contenait une image lors de sa première utilisation, elle contiendra toujours la même image maintenant qu'elle vous a été renvoyée depuis le pool de réutilisation. Vous devez donc réinitialiser toutes les propriétés de la cellule qui ont pu être définies dans un cycle précédent. –
Merci pour cela. Je comprends comment les cellules sont maintenant réutilisées –