ユーザーがセクションごとに1つのアイテムを選択できるように、テーブルを設定しようとしています。
例えば:
- Section 0
- Row 0 √
- Row 1
- Section 1
- Row 0
- Row 1 √
- Row 2
したがって、上記の例では、ユーザーが選択した場合section 0, row 1
、row 0
同じセクションのチェックを外す必要があり、選択した行にチェックマークが付けられます。
選択した行にチェックマークを付ける必要があるセクション1についても同じことが言えます。次に、同じセクションで以前に選択した行からチェックマークを削除します。
- Section 0
- Row 0
- Row 1 √
- Section 1
- Row 0
- Row 1
- Row 2 √
事前定義された数のセクションまたは行がないため、このシナリオでコードが機能することを覚えておいてください。これが私が現在持っているものです、うまくいけば、それは私が正しい道を歩み始めるかもしれないことです。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
MyObject *myObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if ([myObject.optionSelected boolValue] == NO) {
[myObject setOptionSelected:[NSNumber numberWithBool:YES]];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[myObject setOptionSelected:[NSNumber numberWithBool:NO]];
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
if ([tableView numberOfRowsInSection:indexPath.section] > 1) {
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < [tableView numberOfRowsInSection:indexPath.section]; ++i) {
[cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:indexPath.section]]];
}
for (UITableViewCell *deselectCell in cells) {
if ([self.tableView indexPathForCell:deselectCell] != indexPath && deselectCell != cell) {
MyObject *tempObject = [self.fetchedResultsController objectAtIndexPath:[self.tableView indexPathForCell:deselectCell]];
[tempObject setOptionSelected:[NSNumber numberWithBool:NO]];
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
}
}
ご覧のとおり、オブジェクトの選択された状態も設定しているので、これをそのままにしておく必要があります:)
フィードバックとヘルプを事前に感謝します!