TableViewCell重用机制避免重复显示问题
2015-12-22 12:01
357 查看
// 这样配置的话超过页面显示的内容会重复出现 - (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; }<div class="sycode" style="color: rgb(51, 51, 51); margin-bottom: 10px; font-family: 'Microsoft YaHei', Verdana, sans-serif, SimSun; font-size: 13px; letter-spacing: 0.30000001192092896px; line-height: 28px;"><pre name="code" class="sycode" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 10px; margin-bottom: 10px; padding: 5px; font-family: 'Courier New', Arial; border-color: rgb(221, 221, 221) rgb(221, 221, 221) rgb(221, 221, 221) rgb(108, 226, 108); border-style: solid; border-width: 1px 1px 1px 4px; border-image-source: none; background-image: none; background-attachment: scroll; background-color: rgb(246, 246, 246); font-size: 10pt; line-height: 18px; background-position: 0px 0px; background-repeat: repeat repeat;">//通过以下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; }
相关文章推荐
- 【笔记】Linux shell脚本编程
- Guava学习笔记
- 百度JS模板引擎 baiduTemplate 1.0.6 版
- 《虚空之遗》zvt轰炸艇流三开攻略
- 编写一个函数,将2个数字相加,不使用+运算符
- Oracle 日期格式处理
- vsFTP 加密传输-自生存证书
- nfs 错误解决
- 6.1 实现strpbrk的功能
- sparkR could not find function "textFile"
- scope的使用场景和说明
- unterminated string literal
- 2015年最受欢迎的8个用户界面组件
- 一些在开发中不曾注意的小知识,只是怕以后忘了,有迹可查
- IOS版添加phonegap-银联支付插件教程
- 1_网络基础 OSI网际互联(上)
- iOS26 AFNetworking
- Android支付宝支付封装代码
- 在安全层面,企业如何获得更好的投资回报率 ROI?
- Pathon 复制一个集合