【发布时间】:2013-05-15 09:49:56
【问题描述】:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* CellIdentifier = @"MessageCellIdentifier";
MessageTableViewCell* cell = (MessageTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Message* message = [self.dataModel.messages objectAtIndex:indexPath.row];
[cell setMessage:message];
return cell;
}
我正在开发一个聊天应用程序,其中在发送和接收同时发生的消息时出现异常。以下是异常消息
断言失败 -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2380.17/UITableView.m:1070 2013-04-30 16:55:14.314 [2689:907] 由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 第 0 节* 中的行数。
更新后现有节中包含的行数 (19) 必须等于更新前该节中包含的行数 (17),加上或减去从该节中插入或删除的行数(插入 1,删除 0)加上或减去移入或移出该部分的行数(0 移入,0 移出)。'
- (int)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataModel.messages count];
}
- (void)didSaveMsg:(NSMutableArray *)array1
{
self.dataModel.messages = array1;
[tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:array1.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[self scrollToNewestMessage];
}
- (void)scrollToNewestMessage
{
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^(void){
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:(self.dataModel.messages.count - 1) inSection:0];
[tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
});
[tableview reloadData];
[[DBModel database]updateMessageCount1:mobileNo cnt:@"0"];
}
【问题讨论】:
-
一次显示你的 numberOfRows 方法??
-
例外是删除或添加行。只需检查您的 numberOfRows 方法吗?一旦粘贴。
-
添加行
-
只需将您的 numberOfRows 方法粘贴到您的问题中
-
你能粘贴你的代码如何添加行吗?
标签: objective-c