ios之TableViewCell重用机制避免重复显示问题
来源:互联网 发布:游戏编程directx9.0 编辑:程序博客网 时间:2024/05/16 11:42
常规配置如下 当超过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!
0 0
- ios之TableViewCell重用机制避免重复显示问题
- ios之TableViewCell重用机制避免重复显示问题
- ios之TableViewCell重用机制避免重复显示问题
- ios之TableViewCell重用机制避免重复显示问题
- ios之TableViewCell重用机制避免重复显示问题
- ios之TableViewCell重用机制避免重复显示问题
- iOS之TableViewCell重用机制避免重复显示问题
- ios之TableViewCell重用机制避免重复显示方案
- TableViewCell重用机制避免重复显示问题
- TableViewCell重用机制避免重复显示问题
- iOS UITableViewCell重用机制避免重复显示问题
- IOS之TableViewCell重用
- iOS-TableViewCell重用机制分析
- iOS 避免cel重用机制
- iOS 避免cel重用机制
- ios tableViewCell重用
- tableViewCell重用问题总结
- 关于TableViewCell的重用机制
- 芯动技术之一: 如何开发低成本的数据采集模块
- MFC做一个可以自定义背景色的方框
- 第十二周项目1--阅读2
- 关于Ioc & Unify 2.0介绍
- javascript------实现全选、反选、全不选
- ios之TableViewCell重用机制避免重复显示问题
- Windows和Linux下socket的区别
- dashconf.php---dashboard.php总览界面配置文件分析
- Android 编程下 Touch 事件的分发和消费机制
- Oracle的回收站和闪回查询机制(二)
- 第十二周项目1---阅读3
- php 连接mysql 方式 过程式和对象方法(mysqli,mysql_connet)
- Linux 分析应用程序依赖工具
- vector的成员函数解析