HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UITableView和Cell Reuse大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UITableView,我已经将uITableViewCell(称为Customcatell)子类化,因此它有几个标签一个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";


    Customcatell *cell = (Customcatell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[Customcatell 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?

这是我的Customcatell类的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;
}
...

大佬总结

以上是大佬教程为你收集整理的ios – UITableView和Cell Reuse全部内容,希望文章能够帮你解决ios – UITableView和Cell Reuse所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。