gpt4 book ai didi

ios - insertRowsAtIndexPaths 中的 NSInternalInconsistencyException

转载 作者:行者123 更新时间:2023-11-29 02:49:34 31 4
gpt4 key购买 nike

我正在尝试为我的表格 View 实现 insertRowsAtIndexPaths

在此实现之前,我曾经在用户点击表末尾后调用[self.tableView reloadData]。这种方法有点影响我的应用程序质量,因为大量行和大量滚动。(每次重新加载数据之前都会卡住)

所以我使用了这段代码:

p.lastid = appRecord.ids;
[p main];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[self.tableView beginUpdates];
NSArray *temp = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[p.appRecordList count] inSection:0]];
[self.tableView insertRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

});
});
}
return cell;
}

旧的工作代码是:

  NSArray *temp =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
self.entries = temp;
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

p.appRecordList 是一个 NSArray,新行项来自 hayoola 获取。

对于 numberOfRowInSection 我使用这个:

#define kCustomRowCount     10
@property (nonatomic, assign) int intindex;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger count = [self.entries count];
if (count == 0)
{
return kCustomRowCount;
}
return count;
}

此外,我的代码中没有 cellforRowAtIndexPath 。我应该提到的是,我的程序每次都会从服务 r 获取 10 行。

这是控制台错误代码:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 10 into section 0, but there are only 10 rows in section 0 after the update'*

编辑:(因答案而更改)

将更新方法更改为:

[self.tableView beginUpdates];
NSArray *temp_1 =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
self.entries = temp_1;
NSArray *temp = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[p.appRecordList count] inSection:0]];
[self.tableView insertRowsAtIndexPaths:temp withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

insertRowsAtIndexPaths到此:

    NSUInteger count = [self.entries count];
return count;

出现此错误:

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 (20) 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).'*

最佳答案

您需要确保在调用 endUpdates 之前,附加记录已添加到 self.entries 中。异常(exception)情况是告诉您尝试添加第 11 行,但 numberOfRowsInSection 返回 10

您需要确保插入的行数与添加到数组中的元素相同

您的更新方法应该是

[self.tableView beginUpdates];

NSMutableArray *newIndices=[NSMutableArray new];

for (id record in p.appRecordList) {
[self.entries addObject:record];
[newIndicies addObject:[NSIndexPath indexPathForRow:self.entries.count]];
}


[self.tableView insertRowsAtIndexPaths:newIndices withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];

关于ios - insertRowsAtIndexPaths 中的 NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24720649/

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