我有 UISwitch,它在 2 个表视图状态之间切换。问题是快速切换导致崩溃。我认为这是因为删除行需要一些时间(因为动画),如果我添加了一些单元格并且几乎同时尝试删除它们然后崩溃。我能做些什么?我真的很想要动画,所以 [self.tableView reloadData] 不是解决方案。
- (void)switchChangedInIndexPath:(NSIndexPath *)indexPath
{
EBOrderFormCell *cell = (EBOrderFormCell *)[self.tableView cellForRowAtIndexPath:indexPath];
BOOL on = cell.picker.on;
if (indexPath.row == EBOrderFormTakeAway) {
self.takeAway = on;
[self takeAwayChanged];
}
}
- (void)takeAwayChanged
{
NSArray *notTakeAwayCells = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:EBOrderFormStreet inSection:0],
[NSIndexPath indexPathForRow:EBOrderFormHouseNumber inSection:0],
[NSIndexPath indexPathForRow:EBOrderFormFlatNumber inSection:0],
nil];
NSArray *takeAwayCells = [[NSArray alloc] initWithObjects:
[NSIndexPath indexPathForRow:EBOrderFormCafe inSection:0],
nil];
[self.tableView beginUpdates];
if (self.takeAway) {
[self.tableView insertRowsAtIndexPaths:takeAwayCells withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:notTakeAwayCells withRowAnimation:UITableViewRowAnimationFade];
} else {
[self.tableView insertRowsAtIndexPaths:notTakeAwayCells withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:takeAwayCells withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView endUpdates];
}
更新:用代码修复它:
- (void)takeAwayChanged
{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}