gpt4 book ai didi

iOS- deleteRowsAtIndexPaths 崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:22 27 4
gpt4 key购买 nike

我正在尝试使用以下代码删除。

[super deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

它返回多个异常。

 *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070
2013-01-29 16:28:22.628

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 5 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

最佳答案

这是因为你应该有一个返回行数的动态方式。

例如,我创建了一个 3 数组。每个都有 3 个值(这些是 NSArray 变量):

.h文件中:

NSArray *firstArray;
NSArray *secondArray;
NSArray *thirdArray;

.m 文件中,viewDidLoad 或 init 或类似的东西:

firstArray = [NSArray arrayWithObjects:@"Cat", @"Mouse", @"Dog", nil];
secondArray = [NSArray arrayWithObjects:@"Plane", @"Car", @"Truck", nil];
thirdArray = [NSArray arrayWithObjects:@"Bread", @"Peanuts", @"Ham", nil];

当返回表中的行数时,我有:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return array.count;
if (section == 0) {
return firstArray.count;
} else if (section == 1) {
return secondArray.count;
} else {
return thirdArray.count;
}
}

然后,在 cellForRow 中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

if (indexPath.section == 0) {
cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
}

return cell;
}

然后我通过在表格上滑动或您想要删除的其他方式删除@"Dog"。然后,当重新加载表格时,您的数组计数将为 2,因此表格将“知道”它必须只显示 2 行。基本上,您还需要更新数据源。它也适用于其他部分。因为您从数组中删除了元素,所以行数也会更新。

关于iOS- deleteRowsAtIndexPaths 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14581923/

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