1
votes

My app is a IM application, when the application into the background and once again back to the foreground will crush. This is my part of the code。

-(void)uiAddChatroomMessages:(NSArray*)messages{
    NSMutableArray *indexPaths = [[NSMutableArray alloc]init];
    int i = (short)self.messageArray.count ;
    for (RTIMMessage *msg in messages) {
        [self.messageArray addObject:msg];
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
    [indexPaths addObject:indexPath];
    [self.chatMessageTableView beginUpdates];
    [self.chatMessageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.chatMessageTableView endUpdates];
    [self.chatMessageTableView reloadData];
}

Run to this code "[self.chatMessageTableView endUpdates]",It crush and prompt "Thread1:signal SIGABRT".

2016-08-24 15:49:18.500 RTIM_iOS_Demo[1834:1326398] * Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.12/UITableView.m:1716 2016-08-24 15:49:18.590 RTIM_iOS_Demo[1834:1326398] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (62) must be equal to the number of rows contained in that section before the update (57), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' *** First throw call stack: (0x18238adb0 0x1819eff80 0x18238ac80 0x182d10154 0x1876e1808 0x100072ccc 0x1000728b0 0x100095244 0x10009468c 0x100077d20 0x100241a7c 0x100241a3c 0x1002474e4 0x182340d50 0x18233ebb8 0x182268c50 0x183b50088 0x187552088 0x1000a5340 0x181e068b8) libc++abi.dylib: terminating with uncaught exception of type NSException

3
Never call reloadData right after insertRowsAtIndexPaths. The insert method rearranges the table view automatically.vadian

3 Answers

0
votes

In your method you added to self.messageArray some new objects but added just one row. Modify your code to.

-(void)uiAddChatroomMessages:(NSArray*)messages{
    NSMutableArray *indexPaths = [NSMutableArray array];
    NSUInteger i = self.messageArray.count;
    for (RTIMMessage *msg in messages) {
        [self.messageArray addObject:msg];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [indexPaths addObject:indexPath];
        i ++;
    }

    [self.chatMessageTableView beginUpdates];
    [self.chatMessageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.chatMessageTableView endUpdates];
    [self.chatMessageTableView reloadData];
}
0
votes

As per the stack trace it seems like your index path is wrong. You are using array count as row index, try with (array count-1), hope it will work.

0
votes

After inserting you the number returned by numberOfRowsInSection must match with the new expected value. You might be returning a constant or a number of elements of an array which hasn't changed.