UITableViewCell仅在一个部分内选中/取消选中

UITableViewCell仅在一个部分内选中/取消选中,uitableview,nsindexpath,Uitableview,Nsindexpath,我正在尝试设置我的表,以便用户可以为每个部分选择一个项目 例如: - Section 0 - Row 0 √ - Row 1 - Section 1 - Row 0 - Row 1 √ - Row 2 因此,在上面的示例中,如果用户选择了第0节第1行,则应取消选中同一节中的第0行,并且选中的行将获得选中标记 第1节也是如此,在第1节中,任何选中的行都应该有一个复选标记,然后我想从同一节中先前选中的行中删除该复选标记 - Section 0 - R

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

例如:

- Section 0
    - Row 0 √
    - Row 1
- Section 1
    - Row 0
    - Row 1 √
    - Row 2
因此,在上面的示例中,如果用户选择了
第0节第1行
,则应取消选中同一节中的
第0行
,并且选中的行将获得选中标记

第1节也是如此,在第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];
            }
        }
    }
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath{
[self.tableView取消rowatinexpath:indexath:YES];
MyObject*MyObject=[self.fetchedResultsController对象索引路径:indexPath];
UITableViewCell*cell=[self.tableView cellForRowAtIndexPath:indexPath];
如果([myObject.options Selected boolValue]==否){
[myObject SetOptions Selected:[NSNumber numberWithBool:是]];
[单元格设置访问类型:UITableViewCellAccessoryCheckmark];
}否则{
[myObject SetOptions Selected:[NSNumber Number Withbool:NO]];
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
if([tableView numberOfRowsInSection:indexPath.section]>1){
NSMUTABLEARRY*单元格=[[NSMUTABLEARRY alloc]init];
对于(NSInteger i=0;i<[tableView numberOfRowsInSection:indexPath.section];+i){
[cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i目录:indexPath.section]];
}
对于(UITableViewCell*取消选择单元格中的单元格){
if([self.tableView indexPathForCell:descell]!=indexPath&&descell!=cell){
MyObject*tempObject=[self.fetchedResultsController对象索引路径:[self.tableView indexPathForCell:Decell]];
[tempObject SetOptions Selected:[NSNumber numberWithBool:NO]];
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
}
}
}
正如您所看到的,我还在设置对象的选定状态,我希望它保持不变:)


提前感谢您的反馈和帮助

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

NSMutableArray *selectedCellPaths = [[NSMutableArray alloc] init];
然后您的
表视图: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中的内容,但是如果没有小的更改,它应该可以工作。

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

NSMutableArray *selectedCellPaths = [[NSMutableArray alloc] init];
然后您的
表视图: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中的代码,但是如果没有小的改动,它应该可以工作