UITableView缺少新可见行的单元格NSInternalInconsistencyException

时间:2017-05-11 04:37:46

标签: ios objective-c uitableview autolayout uikit

我的应用程序遇到了这个问题,不知道出了什么问题。 致命异常:NSInternalInconsistencyException 缺少新显示的第78行的单元格

这是堆栈跟踪:

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x191bbefd8 __exceptionPreprocess
1  libobjc.A.dylib                0x190620538 objc_exception_throw
2  CoreFoundation                 0x191bbeeac +[NSException raise:format:]
3  Foundation                     0x192656710 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4  UIKit                          0x198022b3c __46-[UITableView _updateWithItems:updateSupport:]_block_invoke.1049
5  UIKit                          0x197d234b8 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:]
6  UIKit                          0x197d39bac +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:]
7  UIKit                          0x197ee8770 -[UITableView _updateWithItems:updateSupport:]
8  UIKit                          0x197eccfbc -[UITableView _endCellAnimationsWithContext:]
9  MyApp                      0x1000ed518 -[UICommentsTableViewController insertNewComment:atLocation:] (UICommentsTableViewController.m:1228)
10 MyApp                      0x1000e99e8 -[UICommentsTableViewController onCommentsReceivedWithMessage:] (UICommentsTableViewController.m:881)
11 MyApp                      0x1000e9094 -[UICommentsTableViewController processWSMessage:] (UICommentsTableViewController.m:803)
12 MyApp                      0x1000e8f84 -[UICommentsTableViewController webSocket:didReceiveMessage:] (UICommentsTableViewController.m:788)
13 MyApp                      0x10009ea30 __30-[SRWebSocket _handleMessage:]_block_invoke (SRWebSocket.m:837)
14 libdispatch.dylib              0x190a769e0 _dispatch_call_block_and_release
15 libdispatch.dylib              0x190a769a0 _dispatch_client_callout
16 libdispatch.dylib              0x190a7b5e8 _dispatch_main_queue_callback_4CF
17 CoreFoundation                 0x191b6d0c0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
18 CoreFoundation                 0x191b6acdc __CFRunLoopRun
19 CoreFoundation                 0x191a9ad94 CFRunLoopRunSpecific
20 GraphicsServices               0x193504074 GSEventRunModal
21 UIKit                          0x197d53130 UIApplicationMain
22 MyApp                      0x10011ba40 main (main.m:7)
23 libdyld.dylib                  0x190aa959c start

错误发生在这里:

- (void)insertNewComment:(NSIndexSet *)indexSet atLocation:(NSInteger)location{

    [self.tableView beginUpdates];

    NSMutableArray *indexArray = [NSMutableArray new];
    if (!indexSet) {
        [indexArray addObject:[NSIndexPath indexPathForRow:location inSection:0]];
    } else {
        [indexSet enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop ) {
            [indexArray addObject:[NSIndexPath indexPathForRow:index inSection:0]];
        }];
    }

    [self.tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationBottom];
    [self.tableView endUpdates];

}

我使用AutoLayout作为单元格,但我手动计算高度并根据设备方向缓存它们。

身高计算器:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


    Comment* comment = self.comments[indexPath.row];

    CGFloat cellHeight = [self getHeightForLayoutMode:self.screenMode forCommentID:comment.commentID];

    if(cellHeight > 0) {

        return cellHeight;
    }


    if([comment isType2]) {
        [self setHeightForLayoutMode:self.screenMode forCommentID:comment.commentID height:106];
        return 106;
    }

    CGFloat width = tableView.bounds.size.width - 100;

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;


    NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"MyFont" size:11.0f], NSParagraphStyleAttributeName:paragraphStyle};

    CGRect rect = [comment.message boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:attributes
                                                context:nil];

    CGFloat height = 31+ceilf(rect.size.height);

    [self setHeightForLayoutMode:self.streamViewController.screenMode forCommentID:comment.commentID height:height];

    return height;
}

有人有这样的问题(Crash with Missing cell for newly visible row when updating UITableView),他们刚刚删除了estimatedSectionHeaderHeight,但我的代码上没有estimatedSectionHeaderHeight。

2 个答案:

答案 0 :(得分:0)

插入值时,如果节数超出范围,则会发生此错误。这不是身高问题。

答案 1 :(得分:0)

我找出了问题的原因。

在我的视图控制器中,我有一个textview,它将触发键盘显示。我将UITableView固定在底部。我在keyboardWillShow&上将它设置为scrollToBottom在textview高度更改时调整插入。一旦键盘显示,桌面视图几乎不可见(特别是在iPhone 5上)。

现在修复是我将tableview的底部固定在textview的顶部,保持相同的高度和高度。 tableview的可见性,我不需要scrollToBottom&再调整插入,因为自动布局会处理它。希望它可以帮助别人!

干杯。