在UITableView IOS中select行时,多选checkMark

我有一个UITableView显示选中行时的复选标记。 问题是,当我在didSelectRowAtIndexPathselect一行并在所选行上添加复选标记时,它会添加一个复选标记。 这是我的代码

任何帮助将非常感激。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; // Configure the cell... cell.textLabel.text=[[Model.category objectAtIndex:indexPath.row] categoryName]; cell.imageView.image=[[Model.category objectAtIndex:indexPath.row]categoryImage]; //cell.detailTextLabel.text =@"Breve Descripción de la categoria"; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if ([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) { [self.tableView cellForRowAtIndexPath:indexPath].accessoryType =UITableViewCellAccessoryNone; [self.cellSelected removeObject:indexPath]; }else { [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark; [self.cellSelected addObject:indexPath]; } [self checkMark]; [tableView reloadData]; } - (void)checkMark{ for (NSIndexPath * indexPath in self.cellSelected) { [self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark; } } 

didSelectRowAtIndexPath中的[self.tableView cellForRowAtIndexPath:indexPath]调用将不会返回确切的单元格。 它可以是同一个小区,新的小区或重复使用的小区。 如果在附属视图中是重复使用的单元格有复选标记,则最终会有两个带有复选标记的单元格。

它更好地存储在arrays中,并相应地使用它。 如果您打算有多个select,请使用下面的代码示例。

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.cellSelected = [NSMutableArray array]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //Cell Initialisation here if ([self.cellSelected containsObject:indexPath]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; //if you want only one cell to be selected use a local NSIndexPath property instead of array. and use the code below //self.selectedIndexPath = indexPath; //the below code will allow multiple selection if ([self.cellSelected containsObject:indexPath]) { [self.cellSelected removeObject:indexPath]; } else { [self.cellSelected addObject:indexPath]; } [tableView reloadData]; } 

只需使用didSelectRowAtIndexPathdidDeselectRowAtIndexPath方法和您的表视图控制器实例即可。

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [[self.tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark]; } -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [[self.tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone]; } 

尝试这个:

在.m文件中声明:

 @interface MyViewController () { NSIndexPath *__selectedPath; } 

tableView:cellForRowAtIndexPath:检查给定的indexPath是否与存储在ivar中相同:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //configure cell if ([__selectedPath isEqual:indexPath]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; } 

tableView:didSelectRowAtIndexPath存储指向选定的NSIndexPath的指针,或者如果单元格已经取消,则为零

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (cell.accessoryType == UITableViewCellAccessoryNone) { cell.accessoryType = UITableViewCellAccessoryCheckmark; __selectedPath = indexPath; } else { cell.accessoryType = UITableViewCellAccessoryNone; __selectedPath = nil; } } 

我在没有检查Xcode的情况下编写它,所以可能会有一些错别字,但是我显示了主要的想法。 在我看来,你不应该打电话给[self checkMark]; 在你的tableView:cellForRowAtIndexPath:方法。

此外,如果您想一次只有一个选定的单元格,则不应该创buildNSMutableArray来存储NSIndexPath 。 看来你的cellSelected了一次2个NSIndexPaths ,这就是为什么你有这个奇怪的行为。