• tableViewCell重用


       转载请注明出处!!!

    常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复或者出现内容不显示的情况。这就涉及到重用了。

    复制代码
     1 // 这样配置的话超过页面显示的内容会重复出现
     2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     3 {
     4     // 定义唯一标识
     5     static NSString *CellIdentifier = @"Cell";
     6     // 通过唯一标识创建cell实例
     7     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     8     // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)
     9     if (!cell) {
    10         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    11     }
    12     // 对cell 进行简单地数据配置
    13     cell.textLabel.text = @"text";
    14     cell.detailTextLabel.text = @"text";
    15     cell.imageView.image = [UIImage imageNamed:@"4.png"];
    16     
    17     return cell;
    18 }
    复制代码

    通过以下3方案可以解决

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

    复制代码
     1 // 方案一  通过不让他重用cell 来解决重复显示
     2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     3 {
     4     // 定义唯一标识
     5     static NSString *CellIdentifier = @"Cell";
     6     // 通过indexPath创建cell实例 每一个cell都是单独的
     7     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     8     // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)
     9     if (!cell) {
    10         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    11     }
    12     // 对cell 进行简单地数据配置
    13     cell.textLabel.text = @"text";
    14     cell.detailTextLabel.text = @"text";
    15     cell.imageView.image = [UIImage imageNamed:@"4.png"];
    16     
    17     return cell;
    18 }
    复制代码

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

    复制代码
     1 // 方案二  同样通过不让他重用cell 来解决重复显示 不同的是每个cell对应一个标识
     2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     3 {
     4     // 定义cell标识  每个cell对应一个自己的标识
     5     NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];
     6     // 通过不同标识创建cell实例
     7     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     8     // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)
     9     if (!cell) {
    10         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    11     }
    12     // 对cell 进行简单地数据配置
    13     cell.textLabel.text = @"text";
    14     cell.detailTextLabel.text = @"text";
    15     cell.imageView.image = [UIImage imageNamed:@"4.png"];
    16     
    17     return cell;
    18 }
    复制代码

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

    复制代码
     1 // 方案三  当页面拉动需要显示新数据的时候,把最后一个cell进行删除 就有可以自定义cell 此方案即可避免重复显示,又重用了cell相对内存管理来说是最好的方案 前两者相对比较消耗内存
     2 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     3 {
     4     // 定义唯一标识
     5     static NSString *CellIdentifier = @"Cell";
     6     // 通过唯一标识创建cell实例
     7     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     8     
     9     // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)
    10     if (!cell) {
    11         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    12     }
    13     else//当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免
    14     {
    15         while ([cell.contentView.subviews lastObject] != nil) {
    16             [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
    17         }
    18     }
    19     // 对cell 进行简单地数据配置
    20     cell.textLabel.text = @"text";
    21     cell.detailTextLabel.text = @"text";
    22     cell.imageView.image = [UIImage imageNamed:@"4.png"];
    23     
    24     return cell;
    25 }
    复制代码
  • 相关阅读:
    初学Python,对于开发工具不是很了解?一文带你选择适合你的开发工具
    Python文学家为Python写的一首词?(附中英文版)
    大数据到底怎么学: 数据科学概论与大数据学习误区
    Python写代码的时候为什么要注释?Sun因此被Oracle收购
    大数据分析:大数据时代如何发现身边的大数据?
    大数据经典学习路线(及供参考)之 一
    关于如何获取移动端 touchmove 事件中真正触摸点下方的元素
    webservice
    VS文件后缀名大全详解
    string 转 char* (C#)
  • 原文地址:https://www.cnblogs.com/weicyNo-1/p/7659823.html
Copyright © 2020-2023  润新知