Pau*_*ner 6 crash uitableview uiswitch ios
我有一个UITableView,它向用户提供了一些设置.除非UISwitch处于"开"位置,否则某些单元格将被隐藏.我有以下代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return switchPush.on ? 6 : 1;
}
// Hooked to the 'Value Changed' action of the switchPush
- (IBAction)togglePush:(id)sender {
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:0];
for(int i = 1; i <= 5; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[tableView beginUpdates];
if(switchPush.on) {
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
} else {
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
[tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)
这可以按预期工作,直到我快速连续两次切换UISwitch(通过双击),在这种情况下应用程序崩溃
Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.
Run Code Online (Sandbox Code Playgroud)
我知道它是由numberOfRowsInSection的错误返回值引起的,因为当单元格动画仍在播放时,开关回到其原始位置.我已经尝试禁用切换并在其他事件处理程序下挂钩代码,但似乎没有任何东西可以防止崩溃.使用reloadData而不是动画也解决了问题,但我更喜欢漂亮的动画.
有人知道实现这个的正确方法吗?
| 归档时间: |
|
| 查看次数: |
2165 次 |
| 最近记录: |