当前位置: 首页 > news >正文

TableViewCell重影问题

UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件。上面主要是一个个的UITableViewCell,可以让UITableViewCell响应一些点击事件,也可以在UITableViewCell中加入UITextField或者UITextView等子视图,使得可以在cell上进行文字编辑。

UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,当cell滚出屏幕时,会将滚出屏幕的单元格放入重用的queue中,当某个未在屏幕上的单元格要显示的时候,就从这个queue中取出单元格进行重用。

但对于多变的自定义cell,有时这种重用机制会出错。比如,当一个cell含有一个UITextField的子类并被放在重用queue中以待重用,这时如果一个未包含任何子视图的cell要显示在屏幕上,就会取出并使用这个重用的cell显示在无任何子视图的cell中,这时候就会出错。

解决方法:

方法1 将获得cell的方法从- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 换为-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,因而只要将它换为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),就可以不使用重用机制,因而问题就可以得到解决,虽然可能会浪费一些空间。

示例代码:


[plain]
- (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]; 
    } 
     //...其他代码                               

- (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]; 
    } 
    //...其他代码 

- (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重用。

示例代码:



- (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的所有子视图 

        for (UIView *view in cell.contentView.subviews) {

            [view removeFromSuperview];

        }

    } 

转载于:https://www.cnblogs.com/MasterPeng/p/5648888.html

相关文章:

  • HDU1048 The Hardest Problem Ever
  • CentOS下配置Hadoop集群:java.net.NoRouteToHostException: No route to host问题的解决
  • Linux 下vsftp配置文件
  • Flymeos插桩适配教程
  • 在liunx中构建DNS主从服务器的配置文档
  • httpd搭建虚拟主机、设置访问认证以及https相关的用法
  • 百度编辑器(Ueditor)最新版(1.4.3.3)插入锚点失败原因分析及BUG修复
  • Restore Volume 操作 - 每天5分钟玩转 OpenStack(60)
  • jsp页面实现局部刷新
  • k8s如何管理Pod
  • 全屏无标题栏解决方法
  • SQL Server 2008 中SQL查询语句中字段值不区分大小写问题处理
  • 初识JavaScript
  • iOS: 如何调节UITabbarItem的图片和文字位置
  • Python学习笔记——文件写入和读取
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • AWS实战 - 利用IAM对S3做访问控制
  • CentOS6 编译安装 redis-3.2.3
  • JavaScript设计模式与开发实践系列之策略模式
  • jquery cookie
  • Linux各目录及每个目录的详细介绍
  • Median of Two Sorted Arrays
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • Spring Boot快速入门(一):Hello Spring Boot
  • 多线程 start 和 run 方法到底有什么区别?
  • 分享自己折腾多时的一套 vue 组件 --we-vue
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 三栏布局总结
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 项目管理碎碎念系列之一:干系人管理
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • !!java web学习笔记(一到五)
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (16)Reactor的测试——响应式Spring的道法术器
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (27)4.8 习题课
  • (bean配置类的注解开发)学习Spring的第十三天
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (剑指Offer)面试题34:丑数
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (图)IntelliTrace Tools 跟踪云端程序
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转) 深度模型优化性能 调参
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • .NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .Net Winform开发笔记(一)
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .net经典笔试题