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