7
私はUITableView
を持っており、UITableViewCell
(それはCustomCell
と呼ばれています)というサブクラスになっているので、いくつかのラベルとUIImageView
があります。UITableViewとセルの再利用
特定のセルのみが実際に画像を表示します。ここで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
をセルの作成/再利用後に設定していますか?ここで
は私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;
}
感謝を!なぜ私は私のCustomCellがinitでhomeImageViewを設定していないときに、なぜそれをしなければならないのか分からないのですが? –
セル再利用のためです。 'dequeueReusableCellWithIdentifier'はすでに使用されているセルを返します。最初の使用時に画像が含まれていた場合、再利用プールから返されるようになり、同じ画像が引き続き含まれます。したがって、以前のサイクルで設定されていたセルのすべてのプロパティをリセットする必要があります。 –
ありがとう。私は今、どのように細胞が再利用されているのか理解しています –