重用单元格时需要注意的问题
来源:互联网 发布:上海cda数据分析师培训 编辑:程序博客网 时间:2024/05/21 17:10
1.单元格的内容重叠。解决办法有以下三个:
(1)取消cell的重用机制,通过indexPath来创建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;}
(2)让每个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;}(3)删除重用的cell的所有子视图,从而得到一个没有特殊格式的cell,供其他cell重用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 定义唯一标识 static NSString *CellIdentifier = @"Cell"; // 通过唯一标识创建cell实例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } else { 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;}
2.重用单元格时,一定要注意所有数据是否都已更新(如果不是所有数据都更新,可能出现数组越界问题)
0 0
- 重用单元格时需要注意的问题
- 使用tableView和CollectionView的时候需要注意的几点(单元格重用)
- 需要注意的问题
- 需要注意的问题
- jxl处理合并单元格时的一个问题,需注意
- jxl处理合并单元格时的一个问题,需注意
- 使用ProGuard时需要注意的问题
- 安装svnmanager 时需要注意的问题
- 选择框架时需要注意的问题
- 降低HWM时需要注意的问题
- 指针使用时需要注意的问题
- 使用cloneNode时需要注意的问题
- 使用pthread时需要注意的问题
- 指针加减时需要注意的问题
- 使用iperf时需要注意的问题
- 开发SDK时需要注意的问题
- 重写方法时需要注意的问题
- NDK开发时需要注意的问题
- 一点基础
- 自定义iOS7导航栏背景,标题和返回按钮文字颜色
- VMware下linux Ubuntu使用NAT共享主机IP
- C++学习一virtual关键字的使用
- Windows下TestLink的安装——基于XAMPP(MySQL+Apache)
- 重用单元格时需要注意的问题
- 【连载】研究EasyUI系统—ProgressBar组件
- Spring AOP技术(基于AspectJ)的XML开发
- 简单粗暴的前端加解密方法
- 欢迎使用CSDN-markdown编辑器
- 二分图最大匹配
- python淘宝爬虫基于requests抓取淘宝商品数据
- 人工智能 - 五子棋人机对战
- PHP程序报错Strict Standards: Only variables should be passed by reference in