【问题标题】:UITableView and Cell ReuseUITableView 和单元格重用
【发布时间】:2011-01-06 21:31:32
【问题描述】:

我有一个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;
}

【问题讨论】:

    标签: ios objective-c uitableview reuseidentifier


    【解决方案1】:

    如果没有要显示的图像,则必须清除图像视图:

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

    【讨论】:

    • 感谢您的款待!虽然我仍然不明白为什么我的 CustomCell 在初始化时没有设置 homeImageView 时我必须这样做?
    • 因为单元格重用。 dequeueReusableCellWithIdentifier 将返回一个已使用的单元格给您。如果它在第一次使用时包含图像,那么它仍然包含相同的图像,因为它从重用池返回给您。因此,您必须重置可能在上一个周期中设置的单元格的所有属性。
    • 谢谢。我了解细胞现在是如何被重复使用的
    【解决方案2】:

    您可以选择实现prepareForReuse:

    Swift 中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 2013-04-07
      相关资源
      最近更新 更多