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]; } }

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

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

转载于:https://www.cnblogs.com/Army598719273/p/5365679.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值