UITableView复用导致数据折叠

本文介绍了一种使用纯代码方式创建自定义UITableViewCell的方法,并解决了在UITableView中复用cell时出现的数据残留问题。通过设置标签和复用视图,确保了cell内容的正确更新。

之前一直xib自定义cell了,今天心血来潮整了个纯代码自定义cell,上下滑动时发现复用的cell原数据未清空,导致cell展示的数据发生重叠,贴一下出错代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier=@"OrderListDetailCell";
    UITableViewCell *cell = [self.tableview dequeueReusableCellWithIdentifier:identifier];
    
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
    UIImageView *goodsImg = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 70)];
    UILabel *goodsName    = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 0, UIScreenWidth-(5+goodsImg.frame.size.width+5+5), 40)];
    goodsName.font = [UIFont systemFontOfSize:14];
    goodsName.numberOfLines = 2;
    
    UILabel * goodsCount = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 50, 100, 15)];
    goodsCount.font = [UIFont systemFontOfSize:14];
    UILabel *goodsPrice = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5+100+5, 50, 180, 20)];
    goodsPrice.font = [UIFont systemFontOfSize:14];
    
    NSDictionary *goodsDic = (NSDictionary *)[_cellGoodsInfo objectAtIndex:indexPath.row];
    //展示商品详情
    [goodsImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LoadImageURL,[goodsDic objectForKey:@"goodsImage"]]]];
    goodsName.text =[NSString stringWithFormat:@"%@",[goodsDic objectForKey:@"goodsName"]];
    goodsCount.text =[NSString stringWithFormat:@"共:%@ 元",[goodsDic objectForKey:@"goodsTotalPrice"]];
    goodsPrice.text =[NSString stringWithFormat:@"件数:%@",[goodsDic objectForKey:@"goodsCount"]];
    
    [cell.contentView addSubview:goodsImg];
    [cell.contentView addSubview:goodsName];
    [cell.contentView addSubview:goodsPrice];
    [cell.contentView addSubview:goodsCount];
    
    cell.backgroundColor = RGBCOLOR(236, 236, 236, 1);
    return cell;
}


修改后为如下代码,问题解决:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *identifier=@"OrderListDetailCell";
    UITableViewCell *cell = [self.tableview dequeueReusableCellWithIdentifier:identifier];
    
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
        UIImageView *goodsImg = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 70)];
        UILabel *goodsName    = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 0, UIScreenWidth-(5+goodsImg.frame.size.width+5+5), 40)];
        goodsName.font = [UIFont systemFontOfSize:14];
        goodsName.numberOfLines = 2;
        
        UILabel * goodsCount = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5, 50, 100, 15)];
        goodsCount.font = [UIFont systemFontOfSize:14];
        UILabel *goodsPrice = [[UILabel alloc] initWithFrame:CGRectMake(5+goodsImg.frame.size.width+5+100+5, 50, 180, 20)];
        goodsPrice.font = [UIFont systemFontOfSize:14];
        [goodsImg setTag:1];
        [goodsName setTag:2];
        [goodsCount setTag:3];
        [goodsPrice setTag:4];
        
        [cell.contentView addSubview:goodsImg];
        [cell.contentView addSubview:goodsName];
        [cell.contentView addSubview:goodsPrice];
        [cell.contentView addSubview:goodsCount];
    }
    
    
    UIImageView *a1 = (UIImageView *)[cell.contentView viewWithTag:1];
    UILabel *a2 = (UILabel *)[cell.contentView viewWithTag:2];
    UILabel *a3 = (UILabel *)[cell.contentView viewWithTag:3];
    UILabel *a4 = (UILabel *)[cell.contentView viewWithTag:4];
    
    NSDictionary *goodsDic = (NSDictionary *)[_cellGoodsInfo objectAtIndex:indexPath.row];
    //展示商品详情
    [a1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LoadImageURL,[goodsDic objectForKey:@"goodsImage"]]]];
    a2.text =[NSString stringWithFormat:@"%@",[goodsDic objectForKey:@"goodsName"]];
    a3.text =[NSString stringWithFormat:@"共:%@ 元",[goodsDic objectForKey:@"goodsTotalPrice"]];
    a4.text =[NSString stringWithFormat:@"件数:%@",[goodsDic objectForKey:@"goodsCount"]];
    
    cell.backgroundColor = RGBCOLOR(236, 236, 236, 1);
    return cell;
}




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值