美文网首页
indexPathForCell: 返回nil的解决办法

indexPathForCell: 返回nil的解决办法

作者: 普罗旺斯的梦 | 来源:发表于2018-08-06 16:38 被阅读0次

    在开发中, 经常会在cell中我们有可能需要获取到当前的cell的indexpath, 常用的方法是下面的方法

    UITableViewCell *cell = (UITableViewCell *)[button superview];
    [self.tableView indexPathForCell:cell];
    

    或者

    UIView *view = button;
    while (view != nil && ![view isKindOfClass:[UITableViewCell class]]) {
      view = [view superview];
     }
    UITableViewCell *cell = (UITableViewCell *)view;
    [self.tableView indexPathForCell:cell];
    

    但有时候我们这个方法返回nil, 而cell并不返回nil。
    原因是当对应的cell是不可见的时候[self.tableView indexPathForCell:cell]会返回nil;
    tableView刷新完全创建好当前的cell才能调用indexPathForCell:这个方法
    解决的方法如下

    NSIndexPath *indexPath = [self indexPathForRowAtPoint:cell.center];
    

    相关文章

      网友评论

          本文标题:indexPathForCell: 返回nil的解决办法

          本文链接:https://www.haomeiwen.com/subject/pxsgvftx.html