2011-01-06 22 views
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; 
} 

答えて

14

からinitメソッドだあなたは、ディスプレイに画像がない場合は、画像の表示をクリアする必要があります御馳走を働いた

... 
if (tempString!=nil) { 
    cell.homeImageView.image = [UIImage imageNamed:tempString]; 
} else { 
    cell.homeImageView.image = nil; 
} 
... 
+0

感謝を!なぜ私は私のCustomCellがinitでhomeImageViewを設定していないときに、なぜそれをしなければならないのか分からないのですが? –

+6

セル再利用のためです。 'dequeueReusableCellWithIdentifier'はすでに使用されているセルを返します。最初の使用時に画像が含まれていた場合、再利用プールから返されるようになり、同じ画像が引き続き含まれます。したがって、以前のサイクルで設定されていたセルのすべてのプロパティをリセットする必要があります。 –

+0

ありがとう。私は今、どのように細胞が再利用されているのか理解しています –

関連する問題