2011-01-06 124 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