TableViewCell重用机制避免重复显示问题

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

// 这样配置的话超过页面显示的内容会重复出现
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 定义惟一标识
    static NSString *CellIdentifier = @"Cell";
    // 经过惟一标识建立cell实例
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用以前的cell,而不会再次初始化)
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    // 对cell 进行简单地数据配置
    cell.textLabel.text = @"text";
    cell.detailTextLabel.text = @"text";
    cell.imageView.image = [UIImage imageNamed:@"4.png"];
    
    return cell;
}


//经过如下3方案能够解决
/*方案一  取消cell的重用机制,经过indexPath来建立cell 将能够解决重复显示问题 不过这样作相对于大数据来讲内存就比较吃紧了
*/
// 方案一  经过不让他重用cell 来解决重复显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 定义惟一标识
    static NSString *CellIdentifier = @"Cell";
    // 经过indexPath建立cell实例 每个cell都是单独的
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用以前的cell,而不会再次初始化)
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    // 对cell 进行简单地数据配置
    cell.textLabel.text = @"text";
    cell.detailTextLabel.text = @"text";
    cell.imageView.image = [UIImage imageNamed:@"4.png"];
    
    return cell;
}
/*
方案二  让每一个cell都拥有一个对应的标识 这样作也会让cell没法重用 因此也就不会是重复显示了 显示内容比较多时内存占用也是比较多的和方案一类
*/
// 方案二  一样经过不让他重用cell 来解决重复显示 不一样的是每一个cell对应一个标识
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 定义cell标识  每一个cell对应一个本身的标识
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];
    // 经过不一样标识建立cell实例
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用以前的cell,而不会再次初始化)
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    // 对cell 进行简单地数据配置
    cell.textLabel.text = @"text";
    cell.detailTextLabel.text = @"text";
    cell.imageView.image = [UIImage imageNamed:@"4.png"];
    
    return cell;
}
/*
方案三 只要最后一个显示的cell内容不为空,而后把它的子视图所有删除,等同于把这个cell单独出来了 而后跟新数据就能够解决重复显示
*/
// 方案三  当页面拉动须要显示新数据的时候,把最后一个cell进行删除 就有能够自定义cell 此方案便可避免重复显示,又重用了cell相对内存管理来讲是最好的方案 前二者相对比较消耗内存
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 定义惟一标识
    static NSString *CellIdentifier = @"Cell";
    // 经过惟一标识建立cell实例
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   
    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用以前的cell,而不会再次初始化)
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    else//当页面拉动的时候 当cell存在而且最后一个存在 把它进行删除就出来一个独特的cell咱们在进行数据配置便可避免
    {
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    // 对cell 进行简单地数据配置
    cell.textLabel.text = @"text";
    cell.detailTextLabel.text = @"text";
    cell.imageView.image = [UIImage imageNamed:@"4.png"];
    
    return cell;
}

事实上最好的方法仍是自定义cell,而后每次从新赋值就行了大数据

相关文章
相关标签/搜索