![](/img/trans.png)
[英]Terminating app due to uncaught exception 'NSInternalInconsistencyException' error
[英]Terminating app due to uncaught exception 'NSInternalInconsistencyException' at
我在先前接受的答案中搜索了类似的错误消息,但它们建议的是在将任何行插入UITableView之前正确更新数据源(我认为我已经在这样做)。 我正在使用UIRefreshControl(refreshControl)刷新我的UITableView(myTable)。 然后numberOfRowsInSection返回我的表(mainArray)的数据数组的长度。 尽管我似乎在开始向表中插入行之前更新了数组数据,但应用程序在[self.myTable endUpdates]处崩溃了; 行给我以下错误消息:
由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第0节中的行数无效。更新(15)之后现有节中包含的行数必须等于该行中包含的行数更新前的部分(10),加上或减去从该部分插入或删除的行数(已插入1,已删除0),加上或减去该部分移入或移出的行数(移入0,移入0) )。
这是我的代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mainArray.count;
}
- (void)refresh:(UIRefreshControl *)refreshControl {
dispatch_queue_t checkQueue = dispatch_queue_create("pull to refresh queue",NULL);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(checkQueue, ^{
NSError *error;
jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://someurlwithjsondata.json"]];
if(jsonData != nil)
{
newData = [NSJSONSerialization
JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves
error:&error];
if (error)
{
[refreshControl endRefreshing];
}
else
{
NSArray *indexPathArray = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:mainArray.count-1 inSection:0], nil];
NSMutableArray *values = newData[@"values"];
for (NSMutableDictionary *value in values)
{
[mainArray addObject:value];
}
dispatch_async(main, ^{
[self.myTable beginUpdates];
[self.myTable insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
[self.myTable endUpdates];
[refreshControl endRefreshing];
});
}
}
else
{
[refreshControl endRefreshing];
}
});
}
问题很简单。 重新加载表时,您将5个对象添加到mainArray
但是您只能通过insertRowsAtIndexPaths:
在表视图中插入一行。 这些需要匹配。 插入与添加到mainArray
一样多的行。
您的代码很容易出错,因为您正在手动添加行。 您也可以自己编写异步代码。 更好的方法是使用AFNetworking的成功块填充“ mainArray”并重新加载表,而不是重新编写所有代码。 它看起来像这样:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
///set mainArray
[self.myTable beginUpdates];
[self.myTable endUpdates];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.