UITableViewCell复用重叠的问题

  • Post author:
  • Post category:其他



更多iOS 学习知识,总结尽在  的墨科技:

传送门


当用到cell重用时 会出现以下问题:只要cell重用了,内容就会覆盖叠加

当cell重用时,就出现了以上问题,叠加






解决办法




一、使用Xib, 进行cell的关联



在controller里注册xib


[_listTableView  registerNib:[UINib nibWithNibName:@”InformationCell” bundle:nil] forCellReuseIdentifier:@”Infor”];


– (InformationCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString * cellIndentifier = @”Infor”;

InformationCell *cell = (InformationCell*)[tableView  dequeueReusableCellWithIdentifier:cellIndentifier forIndexPath:indexPath];

if (  cell == nil ) {

cell = [[InformationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];

cell.backgroundColor = [UIColor clearColor];

cell.contentView.backgroundColor = [UIColor clearColor];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

MessageModel * model = [_messArray objectAtIndex:indexPath.row];

if ( [model isKindOfClass:[MessageModel class]] ) {

[cell loadData:model];

}

return cell;

}







二、使用cell纯代码,注册cell


[_listTableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@”activityCell”]; //


activityCell 自定义的名称




– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”activityCell”];

if (!cell) {

cell = [[ActivityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@”activityCell”];

}

ActivityModel *model = [_array objectAtIndex:indexPath.section];

cell.activityModel = model;

cell.selectionStyle=UITableViewCellSelectionStyleNone;

return cell;

}




关键的一步





cell.m进行创建UILayout时,初始化cell






– (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier


{


self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];


if (self) {


// Initialization code


[self createActivityUI];


}


return self;


}

//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

//    [super setSelected:selected animated:animated];

//

//    [self createActivityUI];

//}




应该使用未注释的代码,进行创建,会正常显示











如果使用了注释的代码,即会产生cell的重用叠加错误












版权声明:本文为u010070526原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。