ios之TableViewCell重用机制避免反复显示问题
2016-01-16 19:49
501 查看
常规配置例如以下 当超过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; }
以上都是个人理解,本人也是菜鸟,有理解不正确的地方希望大家指出。同一时候也希望能对大家起到一定的帮助。!
Thank you!
相关文章推荐
- ios 常见错误整理 持续更新
- ios nsnumber怎么转换成nsstring
- 苹果审核通过后包体大了200多MB!都是Xcode 7的错?
- iOS之 json数据解析
- 更新Windows ActiveX,Ios
- iOS--错误集锦--****is missing a [super ViewDidLoad]call
- iOS 8 自适应 Cell
- ios开发笔记
- 基于SnapKit写的自动计算行高开源库
- iOS 异常处理@try,catch
- IOS级联(省份和城市)
- iOS Xcode编译报错问题解决办法汇总
- ios下,对于position:fixed支持不完美的额解决方案
- iOS开发~CocoaPods使用详细说明
- IOS中程序如何进行推送消息(本地推送,远程推送)
- 转: ios的关于autolayout的设计与实现
- iOS后台定位,实时向服务器发送最新位置
- 那些 iOS 7 中容易被忽视的新特性总结
- iOS触摸事件深入
- 【转】iOS 文件下载及断点续传