gpt4 book ai didi

ios - 由于未捕获的异常 'NSInternalInconsistencyException' 正在终止应用程序

转载 作者:行者123 更新时间:2023-12-02 21:58:44 24 4
gpt4 key购买 nike

我在之前接受的答案中搜索了类似的错误消息,但他们建议的只是在将任何行插入 UITableView 之前正确更新数据源(我认为我已经在这样做了)。我正在使用 UIRefreshControl (refreshControl) 来刷新我的 UITableView (myTable)。 numberOfRowsInSection 按预期返回表(mainArray)的数据数组的长度。尽管我似乎在开始向表中插入行之前更新了数组数据,但应用程序在 [self.myTable endUpdates] 处崩溃;行给我以下错误消息:

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 (15) must be equal to the number of rows contained in that section before the update (10), 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).'

这是我的代码:

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

}

});


}

最佳答案

问题很简单。重新加载表格时,您将向 mainArray 添加 5 个对象,但仅通过 insertRowsAtIndexPaths: 在表格 View 中插入一行。这些需要匹配。插入与添加到 mainArray 中一样多的行。

关于ios - 由于未捕获的异常 'NSInternalInconsistencyException' 正在终止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22382582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com