2012-10-23 105 views
1

我试图设置我的表,以便用户可以为每个部分选择一个项目。UITableViewCell仅在一个部分中选中/取消选中

例如:

- 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]; 
      } 
     } 
    } 
} 

正如你看到的,我也设置对象的选择的状态,我想这是保持不变:)

感谢您的任何反馈,并提前帮助!

回答

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中的内容,但是它应该能够限制小的变化。

相关问题