ユーザーにいくつかの設定を提示するUITableViewがあります。 UISwitchが「オン」の位置になければ、一部のセルは非表示になります。私は、次のコードを持っている:UITableViewCellを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];
}
予想通り、私は(ダブルタップすることにより)急速に連続して2回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.
の場合のアプリケーションがクラッシュしました私はそれがnumberOfRowsInSectionの間違った戻り値によって引き起こされることを知っています。なぜなら、セルアニメーションがまだ再生されている間にスイッチが元の位置に戻るからです。私はトグルを無効にして、他のイベントハンドラの下でコードをフックしようとしましたが、何もクラッシュを防ぐことはできません。アニメーションの代わりにreloadDataを使うことで問題は解決しますが、素敵なアニメーションが好きです。
誰でもこれを実装する正しい方法を知っていますか?
UIToggleとは何ですか?あなたは 'UISwitch'を意味しますか? – Mundi
はい、私は質問を修正しました。 –