UITableView和Cell重用

7
我有一个UITableView,并且我已经创建了一个UITableViewCell的子类(称之为CustomCell),所以它有几个标签和一个UIImageView
只有某些单元格将显示图像。这是我的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类的init方法:

    - (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;
}
2个回答

14

如果没有图像可以显示,您必须清除图像视图:

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

谢谢,它起作用了!尽管我仍然不明白为什么我必须这样做,当我的CustomCell在初始化时没有设置homeImageView? - Dominic Williams
7
由于单元格的重复利用,dequeueReusableCellWithIdentifier会返回已经使用过的单元格。如果它在第一次使用时包含了一个图像,现在从重用池中返回给你时,它仍然会包含相同的图像。因此,你需要重置所有可能在前一个循环中设置的单元格属性。 - Ole Begemann
谢谢你。我现在明白了单元格是如何被重复使用的。 - Dominic Williams

0

可选地,您可以实现prepareForReuse

Swift中:

override func prepareForReuse() {
    cell.homeImageView.image = nil;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接