uitableview的重用重叠问题

时间:2023-03-08 23:37:17
uitableview的重用重叠问题

以前也遇到过。但都不知道怎么就解决了。

今天费了一番功夫找到了最佳解决方案。

对于一些复杂的cell 从来都是用自定义的方法,但是如果复杂的cell里面内容多了。特别是图片加载,那难免会出现重叠重用

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

    //初始化字典
NSDictionary *dict = [[NSDictionary alloc] init];
//赋值字典
  dict = table1arr[indexPath.row]; //标示符
static NSString *cellIdentifier = @"SetUpCell";
SetUpCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"SetUpCell" owner:self options:nil] lastObject]; }
//cell的内容
if(indexPath.row != 4)
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} [cell reloadData:dict]; return cell; }

自定义cell代码

- (void)reloadData:(NSDictionary*)dict
{
//赋值前先清空值。
  //如果是创建的控件可以nil  或者  removeFromSuperview 这样就可以避免重用旧值,导致重用重叠
_image.image = nil;
lbl.text = nil; [_image setImage:[UIImage imageNamed:[dict objectForKey:@"image"]]]; lbl.text = [dict objectForKey:@"label"]; if ([[dict objectForKey:@"label"] isEqualToString:@"接受组邀请"]) { _centerLine.constant = -15; UILabel *lbl1 = [[UILabel alloc] init];
lbl1.frame = CGRectMake(43, lbl.zl_y + lbl.zl_height + 20, kScreenWidth - 50, 20);
lbl1.text = @"若你开启接受组邀请,将允许收到来自其他组的邀请";
lbl1.font = [UIFont systemFontOfSize:11.0];
lbl1.textColor = [UIColor grayColor]; UISwitch *sw = [[UISwitch alloc] init];
sw.frame = CGRectMake(kScreenWidth - 70, 10, 15, 5);
sw.selected = YES;
sw.on = YES;
[sw addTarget:self action:@selector(swselected:) forControlEvents:UIControlEventEditingChanged];
[self addSubview:sw]; [self addSubview:lbl1]; } }

以上内容完全自己在网上找的一些解决方案。然后理解出来的心得。

保存下来,以后要用的时候记得来找