2016-08-24 57 views
1

我的应用程序是一个IM应用程序,当应用程序进入后台并再次回到前台时会粉碎。 这是我的代码的一部分tableview刷新粉碎Thread1:信号SIGABRT

-(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]; 
} 

运行以这个码“[self.chatMessageTableView endUpdates]”,它粉碎,并提示“线程1:信号SIGABRT”。

2016年8月24日15:49:18.500 RTIM_iOS_Demo [1834:1326398] *断言 故障 - [UITableView的_endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources /UIKit/UIKit-3512.60.12/UITableView.m:1716 2016年8月24日15:49:18.590 RTIM_iOS_Demo [1834:1326398] *终止 应用由于未捕获的异常 'NSInternalInconsistencyException', 原因:无效更新:部分0中的行数无效。更新(62) 后现有部分中包含的 行数必须等于该部分中包含的行数 之前更新(57),加或减该行中插入或删除 的行数(插入1个,删除0个),加上或减去移入或移出该部分的行的数量 (移入0 ,0搬出)。' ***第一掷调用堆栈:(0x18238adb0 0x1819eff80 0x18238ac80 0x182d10154 0x1876e1808 0x100072ccc 0x1000728b0 0x100095244 0x10009468c 0x100077d20 0x100241a7c 0x100241a3c 0x1002474e4 0x182340d50 0x18233ebb8 0x182268c50 0x183b50088 0x187552088 0x1000a5340 0x181e068b8)的libC++ abi.dylib:与未捕获 异常类型的终止NSException

+0

**从不**在'insertRowsAtIndexPaths'之后立即调用'reloadData'。插入方法自动重新排列表视图。 – vadian

回答

0

在您的方法中,您添加到self.messageArray一些新的对象,但只添加了一行。修改你的代码。

-(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

非常感谢! – shycie

0

根据堆栈跟踪,它似乎像您的索引路径是错误的。你正在使用数组计数作为行索引,试着用(数组-1),希望它能工作。

0

插入之后,numberOfRowsInSection返回的数字必须与新的期望值匹配。您可能会返回一个常量或数组中没有更改的元素。