ios - 关于 tableView 返回 cell 的问题
大家讲道理
大家讲道理 2017-04-17 13:29:24
[iOS讨论组]
objc    if(indexPath.row == self.items.count-1)
    {
      cell = [tableView dequeueReusableCellWithIdentifier:QuestionCellIdentifier  forIndexPath:indexPath];
    }
    else
    {
      cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];
    }
    return cell;

objc      cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];

    if(indexPath.row == self.items.count-1)
    {
      cell = [tableView dequeueReusableCellWithIdentifier:QuestionCellIdentifier forIndexPath:indexPath];
    }
    return cell;

我认为这两种写法结果上应该没有区别,但在我的项目中,返回的结果却是不同的,第一种写法,返回了正确的结果,第二种写法,进入了判断语句中,然后返回了QuestionCell,但是在显示上,却显示的是CommentCell,然后点击,又刷新成了QuestionCell,我确认是在主线程中执行的。

请问为什么会是这种结果?我猜测是dequeueReusableCellWithIdentifier:indexPath中进行了预加载,是这样么?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(1)
天蓬老师

问题可能隐藏在cell = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];与index.row和self.items.count的调用顺序,因为你调用的时候不能保证它们只是单纯返回值,而没有进行某些修改操作。
可以试试在方法开头先把index.row和self.items.count赋值给局部变量。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号