UITableView internal inconsistency: indexPath cannot be nil in _setupCell:forEditing:atIndexPath:...

如果你遇到了以下错误:

2018-11-03 14:07:00.005337+0800 Project[1149:330700] Status bar could not find cached time string image. Rendering in-process.
2018-11-03 14:07:27.542022+0800 Project[1149:330700] *** Assertion failure in -[UITableView _setupCell:forEditing:atIndexPath:animated:updateSeparators:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.54.4/UITableView.m:13357
2018-11-03 14:07:38.970077+0800 Project[1149:330700] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView internal inconsistency: indexPath cannot be nil in _setupCell:forEditing:atIndexPath:animated:updateSeparators:'
*** First throw call stack:
(0x184ba6d8c 0x183d605ec 0x184ba6bf8 0x185596fa0 0x18e88c05c 0x18e81a86c 0x18e81a544 0x1007ef140 0x18e91564c 0x18f1ef478 0x18f1ef3a8 0x18e91564c 0x18ea36870 0x18e91b700 0x18ea511a8 0x18e9989e0 0x18e98d890 0x18e98c1d0 0x18f16dd1c 0x18f1702c8 0x18f169368 0x184b4f404 0x184b4ec2c 0x184b4c79c 0x184a6cda8 0x186a52020 0x18ea8c758 0x10081a1a0 0x1844fdfc0)
libc++abi.dylib: terminating with uncaught exception of type NSException

那么通常会伴随着这样的打印:

2018-11-03 14:06:59.758203+0800 Project[1149:330700] [Assert] UITableView internal inconsistency: _visibleRows and _visibleCells must be of same length. _visibleRows: {0, 2}; _visibleCells.count: 4, _visibleCells: (
    ">",
    ">",
    ">",
    ">"
)

意思是你在某时刻改动了cell的frame,但是并没有刷新它们,所以实际数据和显示出来的有差别。
譬喻我的操作:

- (void)updateTableCellHeight:(CGFloat)height atIndexPaht:(NSIndexPath *)indexPath {
    if (![self.cellHeightDic[indexPath] isEqualToNumber: @(height)]) {
        [self.cellHeightDic setObject:@(height) forKey:indexPath];
//        [self.tableView reloadData];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}

tableView的cell里嵌套了一个collectionView,而collectionView的item数目不定,所以cell的高度需要根据collectionView变化。而我代码里回调出来了高度,刷新了拥有collectionView的cell后,其他的cell可能因此改变了frame却没有刷新造成了问题。
解决方法是把
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
改成
[self.tableView reloadData];

你可能感兴趣的:(UITableView internal inconsistency: indexPath cannot be nil in _setupCell:forEditing:atIndexPath:...)