1

我正在尝试设置我的表格,以便用户可以在每个部分中选择一项。

例如:

- Section 0
    - Row 0 √
    - Row 1
- Section 1
    - Row 0
    - Row 1 √
    - Row 2

因此,在上面的示例中,如果用户选择,则应取消选中同一部分中的选中行,并且选中的行将获得复选标记section 0, row 1row 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];
            }
        }
    }
}

如您所见,我还在设置对象的选定状态,我希望它保持不变:)

感谢您提前提供任何反馈和帮助!

4

1 回答 1

1

你的 tableview 应该声明一个可变数组来保存当前选择的路径:

NSMutableArray *selectedCellPaths = [[NSMutableArray alloc] init];

那么你tableView:didSelectRowAtIndexPath:应该看起来像这样

- (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];
        [selectedCellPaths addObject:indexPath];
    } else {
        [myObject setOptionSelected:[NSNumber numberWithBool:NO]];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        if ([selectedCellPaths containsObject:indexPath]) {
            [selectedCellPaths removeObject:indexPath];
        }
    }

    // Now we're going to remove all but the cell path that is actually selected.

    NSMutableArray *cellsToRemove = [[NSMutableArray alloc] init];
    for (NSIndexPath *selectedCellIndexPath in selectedCellPaths) {
        if ([selectedCellIndexPath compare:indexPath] != NSOrderedSame && selectedCellIndexPath.section == indexPath.section) {
        // deselect cell at selectedCellPath
            [cellsToRemove addObject:selectedCellIndexPath];
        }
    }
    [selectedCellPaths removeObjectsInArray:cellsToRemove];
}

请注意,我刚刚添加了一条评论,您希望在未选择的单元格路径中取消选择实际单元格。您需要自己填写该代码。

我没有测试过这段代码,只是修改了你在 TextMate 中的内容,但它应该可以工作,除非有微小的变化。

于 2012-10-24T15:52:48.573 回答