gpt4 book ai didi

iphone - reloadRowsAtIndexPaths 时崩溃,但 reloadData 时不会崩溃

转载 作者:行者123 更新时间:2023-12-03 18:31:14 32 4
gpt4 key购买 nike

我有一个detailViewController和一个MasterViewController。 MasterViewController 是我拥有 UITableView 的地方。我的表格中有两个部分,顶部部分用于项目列表,底部部分有一个条目,@“添加新行”。

@“添加新行”转到可以编辑 UITextField 的DetailViewController。然后,当按下“保存”时,我会这样做:

- (IBAction)saveButtonPressed:(id)sender {
if ([detailControllerDelegate respondsToSelector:@selector(setNewmap:)]) {
if (self.textField.text.length > 0) {
[self.textField endEditing:YES];
[detailControllerDelegate setValue:self.textField.text forKey:@"newmap"];
[self.navigationController popViewControllerAnimated:YES];
}
}
}

然后在MasterViewController中,

- (void)setNewmap:(NSString *)newmap {
if (![newmap isEqualToString:_newmap]) {
_newmap = newmap;

[self.maps addObject:_newmap];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.maps count] inSection:MAPS_SECTION];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}

它在 reloadRowsAtIndexPaths 上崩溃并显示以下消息:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 3 from section 0 which only contains 2 rows before the update'

我不确定为什么会调用它。我认为当你执行 tableViewUpdates 时,你会更新模型,我使用 self.maps addObject:newmap]; 来更新模型。我记录了计数,计数是正确的。更新模型后,我认为您可以自由地重新加载行。

如果我不重新加载特定行,而只是在 tableView 上调用 reloadData,那么它不会崩溃。不确定这里有什么区别。就像当我更新 self.maps 模型时 numberOfRowsInSection 委托(delegate)方法没有更新一样。

最佳答案

我知道这个问题被问起已经有一段时间了,但我认为问题可能是,reloadRowsAtIndexPaths 依赖于表中这些行的先前存在,并在内部删除、重新创建它们并将它们插入回(在兜帽)。因此,当您在表中没有这些行的情况下调用特定的重新加载时,它会尝试删除不存在的行。

我在 Collection View 中遇到了同样的问题。完全重新加载之所以有效,是因为它会刷新整个内容,并且不依赖于先前的状态,仅依赖于数据源的当前状况。

这是我对这个问题的看法。

关于iphone - reloadRowsAtIndexPaths 时崩溃,但 reloadData 时不会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10844306/

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