ios – UITableView和Cell Reuse

我有一个UITableView,我已经将UITableViewCell(称为CustomCell)子类化,因此它有几个标签一个UI ImageView.

只有某些单元格才能显示图像.这是我的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;
}

解决方法

如果没有要显示的图像,则必须清除图像视图:
...
if (tempString!=nil) {
    cell.homeImageView.image = [UIImage imageNamed:tempString];
} else {
    cell.homeImageView.image = nil;
}
...

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...