美文网首页
cellForRowAtIndexPath return nil

cellForRowAtIndexPath return nil

作者: oncezou | 来源:发表于2016-07-05 20:55 被阅读2304次
    CustomTableCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    

    偶尔会返回nil,原因在于:An object representing a cell of the table, or nil if the cell is not visible or indexPath is out of range.所以当你的 cell 是不可见的时候就会返回nil

    解决方法:重新获取

    CustomTableCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (indexPath) {
        //定于到该行cell(此方法可以用于解决cell被遮挡的问题)
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
    if (cell) {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(indexPathForCell:) object:nil];
    }else{
        [self performSelector:@selector(indexPathForCell:) withObject:indexPath afterDelay:.5];
    }
    
    - (void)indexPathForCell:(id)object {
        if ([object isKindOfClass:[NSIndexPath class]]) {
            NSIndexPath * indexPath = (NSIndexPath *)object;
            CustomTableCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
        }
    }
    

    相关文章

      网友评论

          本文标题:cellForRowAtIndexPath return nil

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