如何在iOS,table C的tableview中的每个部分中仅实现单个单元格选择

我有一个包含5个部分的表视图,我已将tableview选项设置为多个。 每个部分都有不同的行数。 我想要的是设置用户只能从每个部分中选择一个单元格(在我的表格中,用户可以选择任意数量的单元格)。 例如:5个切片中的5个细胞。

从任何部分选择多个单元格应该是不可能的。 如果用户从同一部分选择另一个单元格,则应取消选择先前选择的单元格。 我怎样才能做到这一点。 这是didSelectRowAtIndexPath的示例实现。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { HoteldetalcelloneTableViewCell *cellone = (HoteldetalcelloneTableViewCell *)[self.detailtableView cellForRowAtIndexPath:indexPath]; HoteldetailcelltwoTableViewCell *celltwo = (HoteldetailcelltwoTableViewCell *)[self.detailtableView cellForRowAtIndexPath:indexPath]; //I have implement for two sections to test. if(indexPath.section == 0) { HotelDetailsone *secone = [roomonearray objectAtIndex:indexPath.row]; HoteldetailsforBooking *book = [HoteldetailsforBooking new]; if([secone.offerallow isEqualToString:@"True"]) { celltwo.selectedsignLabel.hidden = NO; } else { cellone.selectedsignLabelone.hidden = NO; } // [self.detailtableView reloadData]; NSLog(@"price for room 1 : %@", secone.itempriceText); } else { HotelDetailsone *sectwo = [roomtwoarray objectAtIndex:indexPath.row]; HoteldetailsforBooking *book = [HoteldetailsforBooking new]; if([sectwo.offerallow isEqualToString:@"True"]) { celltwo.selectedsignLabel.hidden = NO; } else { cellone.selectedsignLabelone.hidden = NO; } // [self.detailtableView reloadData]; NSLog(@"price for room 1 : %@", sectwo.itempriceText); } } 

您需要跟踪细胞的选择。 因此,您需要在数组中存储选定的indexpath

ViewController.h声明这样的属性

 @property(nonatomic,strong) NSMutableDictionary *selectionData; 

现在在ViewController.m

 - (void)viewDidLoad { [super viewDidLoad]; self.selectionData=[[NSMutableDictionary alloc]init]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"]; if ([self.selectionData objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section] ] != nil) { NSMutableArray *sectionData=[[self.selectionData objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]] mutableCopy]; if (![sectionData containsObject:[NSNumber numberWithLong:indexPath.row]]) { cell.accessoryType = UITableViewCellAccessoryNone; cell.numberlabel.text = @"2"; } else { cell.numberlabel.text = @"***"; cell.accessoryType=UITableViewCellAccessoryCheckmark; } } else { cell.numberlabel.text = @"2"; cell.accessoryType = UITableViewCellAccessoryNone; } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"selected section :%li ---> selected row :%li",(long)indexPath.section, (long)indexPath.row); [self handleSelectionForSection:indexPath.section row:indexPath.row]; [self.tablev reloadData]; } -(void)handleSelectionForSection:(long)sectionIndex row:(long)rowIndex { if ([self.selectionData objectForKey:[NSString stringWithFormat:@"%ld",sectionIndex] ] != nil) { NSMutableArray *sectionData=[[self.selectionData objectForKey:[NSString stringWithFormat:@"%ld",sectionIndex]] mutableCopy]; if (![sectionData containsObject:[NSNumber numberWithLong:rowIndex]]) { //removing previous selected rows [sectionData removeAllObjects]; [sectionData addObject:[NSNumber numberWithLong:rowIndex]]; [self.selectionData setObject:sectionData forKey:[NSString stringWithFormat:@"%ld",sectionIndex]]; } else { //cell you tapped is already selected, // you can deselect it by removing object //if you dont want to deselect it comment following lines [sectionData removeObject:[NSNumber numberWithLong:rowIndex]]; [self.selectionData setObject:sectionData forKey:[NSString stringWithFormat:@"%ld",sectionIndex]]; } } else { //section key not available so we need to create it NSMutableArray *sectionData=[[NSMutableArray alloc]init]; [sectionData addObject:[NSNumber numberWithLong:rowIndex]]; [self.selectionData setObject:sectionData forKey:[NSString stringWithFormat:@"%ld",sectionIndex]]; } NSLog(@"All Selection : %@",self.selectionData); } 

您的numberOfRowsInSectionnumberOfSectionsInTableViewtitleForHeaderInSection将保持不变。

如果您有任何疑问,请告诉我。

您可以从界面构建器设置tableview的selection属性。 在IB中选择您的tableview,然后选择attribute inspector and set单个选择attribute inspector and set to selection`属性,如下面的屏幕截图所示。

在此处输入图像描述

或者你可以用编程方式设置,

  self.tableView.allowsMultipleSelection = NO; 

更新:

如果您希望每个部分选择一个,那么您可以实现willSelectRowAtIndexPath ,如下所示,

  - (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath { for ( NSIndexPath* selectedIndexPath in tableView.indexPathsForSelectedRows ) { if ( selectedIndexPath.section == indexPath.section ) [tableView deselectRowAtIndexPath:selectedIndexPath animated:NO] ; } return indexPath ; } 

在这种情况下,你应该允许在tableview进行多项选择。

参考: John Sauer的回答

看起来你正在更新表选择中的celltwo / cellone selectedsignLabel.hidden。 所以@Lion解决方案将无效。 您必须使用以下代码保存上次选择的索引:

 @property (nonatomic, strong) NSMutableDictionary *selectedIndexPathDict; // in viewDidLoad: self.tableView.allowsMultipleSelection = YES; self.selectedIndexPathDict = [[NSMutableDictionary alloc] init]; //In table view delegate. - (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath { NSString *indexSection = [NSString stringWithFormat:@"%ld", (long)indexPath.section]; NSIndexPath *indexPath1 = self.selectedIndexPathDict[indexSection]; if ( indexPath1) { HotelDetailsone *secone = [roomonearray objectAtIndex:indexPath.row]; secone.offerallow ^= YES; //toggle bool value // update new selected index path. [self.selectedIndexPathDict setObject:indexPath forKey:indexSection]; //reload previous selected cell. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{ [tableView beginUpdates]; [tableView reloadRowsAtIndexPaths:@[indexPath1] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; }); } else { //initialise selected index path self.selectedIndexPathDict[indexSection] = indexPath; } [tableView deselectRowAtIndexPath:indexPath animated:NO] ; return indexPath ; } 

我没有更新完整的工作代码。 但这是实现的方法。 您还可以使用userdefault而不是self.selectedIndexPathDict。