iOS中TableViewController的cell复用
来源:互联网 发布:渠道管控管理系统源码 编辑:程序博客网 时间:2024/06/17 23:56
cell内容数据错乱的解决方法如下.
三种方法各有利端,
方法1 将获得cell的方法从- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 换为-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,因而只要将它换为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),就可以不使用重用机制,因而问题就可以得到解决,虽然可能会浪费一些空间。示例代码:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出 if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其他代码 } 方法2 通过为每个cell指定不同的重用标识符(reuseIdentifier)来解决。重用机制是根据相同的标识符来重用cell的,标识符不同的cell不能彼此重用。于是我们将每个cell的标识符都设置为不同,就可以避免不同cell重用的问题了。示例代码:[plain]- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //...其他代码 } 方法3 删除重用cell的所有子视图这个方法是通过删除重用的cell的所有子视图,从而得到一个没有特殊格式的cell,供其他cell重用。示例代码:[plain]- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } else { //删除cell的所有子视图 while ([cell.contentView.subviews lastObject] != nil) { [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; } } //...其他代码 }
阅读全文
0 0
- iOS中TableViewController的cell复用
- IOS 只有在TableViewController里面才可以使用TableView的static cell
- ios tableViewController
- 我的那些bug tableViewController-cell显示数据
- 如何在使用storyboard中使用tableViewController中的Prototype Cells 自定义cell
- 【iOS编程之路】界面设计--1.TableViewController的应用
- 顶] 【iOS编程之路】界面设计--1.TableViewController的应用
- iOS 点击cell同步cell中添加的UIButton控件
- IOS开发 tableview中cell的用法
- IOS开发 tableview中cell的用法
- IOS开发 tableview中cell的用法
- ios UITableView中Cell的重用机制
- ios UITableView中Cell的操作
- IOS中TableView的cell显示问题
- IOS中点击TableView的Cell跳转
- iOS中cell的属性设置
- IOS开发 tableview中cell的用法
- ios UITableView中Cell的重用机制
- 从源数据库抽取数据到中间库
- 项目开发技术框架
- linux-安装使用问题汇总
- Oracle常用语句
- kotlin.KotlinNullPointerException
- iOS中TableViewController的cell复用
- Hibernate实战(第二版)笔记----第三章--域模型与元数据
- 机器学习sklearn库的使用--部署环境(python2.7 windows7 64bit)
- wingIDE Pro6 破解教程
- AJAX加FormDaTA实现文件上传[laravel框架下实现]
- ArrayList的简单实现(手写)
- OpenCV自学笔记10:视频中分割出圆形
- 分布式环境Session处理问题
- 各主流浏览器内核介绍