Cell重用时数据混乱的管理方法

UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件。上面主要是一个个的UITableViewCell,可以让UITableViewCell响应一些点击事件,也可以在UITableViewCell中加入UITextField或者UITextView等子视图,使得可以在cell上进行文字编辑。

UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,当cell滚出屏幕时,会将滚出屏幕的单元格放入重用的queue中,当某个未在屏幕上的单元格要显示的时候,就从这个queue中取出单元格进行重用。

但对于多变的自定义cell,有时这种重用机制会出错。比如,当一个cell含有一个UITextField的子类并被放在重用queue中以待重用,这时如果一个未包含任何子视图的cell要显示在屏幕上,就会取出并使用这个重用的cell显示在无任何子视图的cell中,这时候就会出错。

常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *identifier=@"aaa";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

    if (indexPath.row%2 == 0) {

        cell.textLabel.text = [dataArr objectAtIndex:indexPath.row];

        cell.imageView.image = [photoArr objectAtIndex:indexPath.row];

        cell.backgroundColor = [UIColor greenColor];

    }else

    {

        

        self.leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(280, 0, 40, 40)];

        self.leftLabel.text = [dataArr objectAtIndex:indexPath.row];

        [cell addSubview:self.leftLabel];

        self.leftimageView = [[UIImageView alloc]initWithFrame:CGRectMake(320, 0, 66, 44)];

        self.leftimageView.image = [photoArr objectAtIndex:indexPath.row];

        [cell addSubview:self.leftimageView];

        cell.backgroundColor = [UIColor yellowColor];

    

    }

    return cell;

    

}

 

方案一  取消cell的重用机制,通过indexPath来创建cell 将可以解决重复显示问题 不过这样做相对于大数据来说内存就比较吃紧了

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell == nil) {

       cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    

       if (indexPath.row%2 == 0) {

        cell.textLabel.text = [dataArr objectAtIndex:indexPath.row];

        cell.imageView.image = [photoArr objectAtIndex:indexPath.row];

        cell.backgroundColor = [UIColor greenColor];

    }else

    {

        

        self.leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(280, 0, 40, 40)];

        self.leftLabel.text = [dataArr objectAtIndex:indexPath.row];

        [cell addSubview:self.leftLabel];

        self.leftimageView = [[UIImageView alloc]initWithFrame:CGRectMake(320, 0, 66, 44)];

        self.leftimageView.image = [photoArr objectAtIndex:indexPath.row];

        [cell addSubview:self.leftimageView];

        cell.backgroundColor = [UIColor yellowColor];

    

    }

    return cell;

    

}

方案二  让每个cell都拥有一个对应的标识 这样做也会让cell无法重用 所以也就不会是重复显示了 显示内容比较多时内存占用也是比较多的和方案一类似

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

     NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    if (indexPath.row%2 == 0) {

        cell.textLabel.text = [dataArr objectAtIndex:indexPath.row];

        cell.imageView.image = [photoArr objectAtIndex:indexPath.row];

        cell.backgroundColor = [UIColor greenColor];

    }else

    {

        

        self.leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(280, 0, 40, 40)];

        self.leftLabel.text = [dataArr objectAtIndex:indexPath.row];

        [cell addSubview:self.leftLabel];

        self.leftimageView = [[UIImageView alloc]initWithFrame:CGRectMake(320, 0, 66, 44)];

        self.leftimageView.image = [photoArr objectAtIndex:indexPath.row];

        [cell addSubview:self.leftimageView];

        cell.backgroundColor = [UIColor yellowColor];

    

    }

    return cell;

    

}

方案三 只要最后一个显示的cell内容不为空,然后把它的子视图全部删除,等同于把这个cell单独出来了 然后跟新数据就可以解决重复显示

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    if (indexPath.row%2 == 0) {

        cell.textLabel.text = [dataArr objectAtIndex:indexPath.row];

        cell.imageView.image = [photoArr objectAtIndex:indexPath.row];

        cell.backgroundColor = [UIColor greenColor];

    }else

    {

        

        self.leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(280, 0, 40, 40)];

        self.leftLabel.text = [dataArr objectAtIndex:indexPath.row];

        [cell addSubview:self.leftLabel];

        self.leftimageView = [[UIImageView alloc]initWithFrame:CGRectMake(320, 0, 66, 44)];

        self.leftimageView.image = [photoArr objectAtIndex:indexPath.row];

        [cell addSubview:self.leftimageView];

        cell.backgroundColor = [UIColor yellowColor];

    

    }

    return cell;

    

}

原文地址:https://www.cnblogs.com/lmg4819/p/4810334.html