常规配置如下 当超过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 }

IOS之TableViewCell重用机制避免重复显示问题的更多相关文章

  1. iOS开发之--TableViewCell重用机制避免重复显示问题

    常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复 // 这样配置的话超过页面显示的内容会重复出现 - (UITableViewCell *)tableView:(U ...

  2. 52.tableViewCell重用机制避免重复显示问题

    表刷新超出页面显示的内容会重复出现 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInd ...

  3. ios之TableViewCell重用机制避免反复显示问题

    常规配置例如以下 当超过tableView显示的范围的时候 后面显示的内容将会和前面反复 // 这样配置的话超过页面显示的内容会反复出现 - (UITableViewCell *)tableView: ...

  4. IOS之TableViewCell重用机制解决上下刷新重复显示

    首先我是一个经验浅薄的iOS开发人员,这个问题想必许多初学者经常遇到这些问题,在面试中也会经常问到.现在我们一一解决. 首先我们要知道TableViewCell重用机制的原理是什么,我们抽象的理解为古 ...

  5. IOS开发—UITableView重用机制的了解

    引言 对于一个UITableView而言,可能需要显示成百上千个Cell,如果每个cell都单独创建的话,会消耗很大的内存.为了避免这种情况,重用机制就诞生了. 假设某个UITableView有100 ...

  6. ios UITableView中Cell重用机制导致内容重复解决方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  7. iOS之UITableView中的cell因为重用机制导致新的cell的数据出现重复或者错乱

      UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,当cell滚 ...

  8. iOS基础篇(十三)——UITableView(一)重用机制

    UITableView是app开发中常用到的控件,功能很强大,常用于数据的显示.在学习UITableView使用之前,我们先简单了解一下: 1.UITableView的重用机制 UITableView ...

  9. IOS中UITableViewCell的重用机制原理

    创建UITableViewController子类的实例后,IDE生成的代码中有如下段落: - (UITableViewCell *)tableView:(UITableView *)tableVie ...

随机推荐

  1. 4、第一个JAVA程序(Hello World)

    第一步: 新建一个文本文档,在里面输入内容 public class HelloWorld { public static void main(String[] args){ System.out.p ...

  2. spring aop的两种写法aspect和advisor

    本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html 首先看个例子,如下 接口代码: package com.lei.demo.aop.schema ...

  3. java Map Set遍历

    Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是Map中key值的集合:en ...

  4. JProfiler 解决 Java 服务器的性能跟踪

    作者:徐建祥(netpirate@gmail.com) 时间: 2006/01/05 来自:http://www.anymobile.org 1.摘要......................... ...

  5. 用C/C++开发android应用

    在某些情况下,比如原来与很多c/c++的代码, 可能希望采用c/c++编写android应用程序.在这种情况下,一般使用NDK.但是由于android直提供了java接口,因此不能够直接调用andro ...

  6. PHP 与 UTF-8

    没有一行式解决方案.小心.注意细节,以及一致性. PHP 中的 UTF-8 糟透了.原谅我的用词. 目前 PHP 在低层次上还不支持 Unicode.有几种方式可以确保 UTF-8 字符串能够被正确处 ...

  7. define() vs const 该如何选择?

    使用 define(),除非考虑到可读性.类常量.或关注微优化 1.在 PHP 中是使用 define() 函数来定义常量,PHP 5.3.0 以后,PHP 中也能够使用 const 关键字来声明常量 ...

  8. HDUOJ---------(1045)Fire Net

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. iPhone手机屏幕的尺寸180330更新

    以下是 iPhone的型号和对应的屏幕宽高 英寸  宽 高  厚度 3.5   320 480 4s      ipad   系列   4   320 568 5   5s   4.7  375 66 ...

  10. 仿京东左侧菜单 hover效果-简易demo

    简单描述: 用到的知识点 css 中的绝对定位 以及 Js 中的事件冒泡(或事件委托) .cont{display:inline-block;width:200px;height:200px;bord ...