2011-01-06 135 views
7

我有一個UITableView和我已經subclassed UITableViewCell(稱爲CustomCell),所以它有幾個標籤和UIImageViewUITableView和細胞再利用

只有特定的單元格纔會顯示圖像。這裏是我的代碼爲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; 
} 

回答

14

您,如果您有沒有圖像顯示清除圖像視圖:

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

感謝奏效一種享受!雖然我仍然不明白爲什麼當我的CustomCell在init中沒有設置homeImageView時我必須這樣做? – 2011-01-06 21:41:48