解决tableView cellForRowAtIndex 返回nil 的问题

   **版权所有,请大家尊重原创劳动,转载请注明出处**
   最近在做项目中遇到一个蛋疼的问题,网上查找各种资料,仍不得解。最后偶然的一次尝试才终于碾死这只臭虫(bug)。
   当时现场情况是这样的。在项目中,我在一个页面push 到一个设置页面(自定义的viewController,不是系统的设置页面),里边有一些常见的app设置项,像帮助了,关于了,反馈了,等等一些乱七八糟的东西。
   ![图1-1](https://img-blog.csdn.net/20150518145652471)
   鉴于列表结构简单,清除缓存一行cell上再加个label就OK了。label显示app缓存的文件大小,点击那一行时label显示“正在清理”,清理完毕显示缓存大小。所以label就有这3种状态。这样只要能够找到label就能够改变其显示内容。从而达到我们想要的效果。
   cellForRow代码如下
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer = @"setIdentifer";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:identifer];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 120, 10, 80, HEIGHTOFTABLE - 10*2)];
        label.tag = TagOfLabel;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18.0f];
        label.textColor = [UIColor blackColor];
        [label setHidden:YES];
        [cell.contentView addSubview:label];

    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titleArray[indexPath.row];
    if ([self.titleArray[indexPath.row] isEqualToString:@"清除缓存"])
    {
        UILabel *label = (UILabel *)[cell.contentView viewWithTag:TagOfLabel];
        label.hidden = NO;
    }
    return cell;

}
   小心了,**臭虫**出现。
   在我建好tableView之后通过*cellForRowAtIndexPath*方法去找这个清除缓存的cell时,却返回了nil(indexPath没问题)。这让我很郁闷,于是各种查资料,但并没有发现*cellForRowAtIndexPath*这个方法使用时的特别之处。[stackOverflow也没找到解决办法](http://stackoverflow.com/)。
   首先,可能有些人还没单独调用*cellForRowAtIndexPath*这个方法来定位某一行cell。它是UITableview 的代理方法之一,但它也可以用来返回某一行cell。你可以在*viewDidLoad*等方法里调用,得到cell。从而对cell进行单独设置。
   用法如下:
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:4 inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];
   我们配置一下*indexpath*就行了,如果是自定义的cell,强转一下就可以了。
   对于返回nil的原因我还不得而知,重新建立demo,遗憾的是并未出现类似的问题。 
   [demo在此](http://download.csdn.net/detail/oximing1/8712527)
   最后说一下解决办法,很简单。
   **延迟执行**
   延迟多长时间不重要,姑且延迟0.01s执行。
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:4 inSection:0];
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];
        UILabel *label = (UILabel *)[cell.contentView viewWithTag:TagOfLabel];
        label.backgroundColor = [UIColor brownColor];
    });
    在项目确实解决了问题。所以建议大家调用该方法时也尽量这样写,以防不测。
    OK,bug解决,希望可以帮到有此问题的小伙伴,欢迎大家批评指正。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值