我有以下代码:
@property (nonatomic, strong) NSMutableArray *rows; // pre-filled with 2 rows
// ...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.rows.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *newRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
MyRowRepresentation *newRowRepresentation = [MyRowRepresentation new];
[self.rows insertObject:newRowRepresentation atIndex:newIndexPath.row];
NSMutableIndexSet *indicesToRemove = [NSMutableIndexSet indexSet];
NSMutableArray<NSIndexPath*> *indexPathsToRemove = [NSMutableArray array];
for (int i = 0; i < self.row.count; i++)
{
MyRowRepresentation *mrr = self.rows[i];
if (mrr != newRowRepresentation && [self meetsDeletionRequirements:mrr])
{
[indicesToRemove addIndex:i];
[indexPathsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
}
}
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:@[newRowIndexPath] withRowAnimation:UITableViewRowAnimationTop];
[self.rows removeObjectsAtIndexes:indicesToRemove];
[tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates]; // crash here
}
当我选择一行时,我明白了:
2015-12-14 20:27:17.761 Now[1685:1753114] ERROR CRASH #(null) attempt to insert row 3 into section 1, but there are only 3 rows in section 1 after the update
2015-12-14 20:27:17.828 Now[1685:1753114] ERROR Stack Trace: (
0 CoreFoundation 0x0000000185684248 <redacted> + 160
1 libobjc.A.dylib 0x00000001970a80e4 objc_exception_throw + 60
2 CoreFoundation 0x00000001856840ec <redacted> + 0
3 Foundation 0x0000000186538ed4 <redacted> + 112
4 UIKit 0x000000018a2e57ec <redacted> + 5256
5 Now 0x0000000100092914 -[MyTableViewController tableView:didSelectRowAtIndexPath:] + 1234
...
)
2015-12-14 20:44:48.933 Now[1692:1754780] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 3 into section 1, but there are only 3 rows in section 1 after the update'
但我感到困惑;以下步骤应该有效,不是吗?
要使应用程序崩溃,我选择原始的2行之一,然后选择另一行。
我不明白是什么导致这种情况崩溃?
答案 0 :(得分:0)
我无法回答为什么,但我确实通过使用单个重新加载调用而不是多个插入/删除调用来解决此问题:
@property (nonatomic, strong) NSMutableArray *rows; // pre-filled with 2 rows
// ...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.rows.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
NSIndexPath *newRowIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
MyRowRepresentation *newRowRepresentation = [MyRowRepresentation new];
[self.rows insertObject:newRowRepresentation atIndex:newIndexPath.row];
NSMutableIndexSet *indicesToRemove = [NSMutableIndexSet indexSet];
NSMutableArray<NSIndexPath*> *indexPathsToRemove = [NSMutableArray array];
for (int i = 0; i < self.row.count; i++)
{
MyRowRepresentation *mrr = self.rows[i];
if (mrr != newRowRepresentation && [self meetsDeletionRequirements:mrr])
{
[indicesToRemove addIndex:i];
[indexPathsToRemove addObject:[NSIndexPath indexPathForRow:i inSection:indexPath.section]];
}
}
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
}