在 UITableView IOS 中选择行时的多个复选标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23727255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:09:15  来源:igfitidea点击:

Multiple checkMark when row selected in UITableView IOS

iosobjective-cuitableviewdidselectrowatindexpath

提问by Klinkert0728

I have a UITableViewthat displays checkmarks when a row is selected. The problem is that When i select a row in didSelectRowAtIndexPathand add a checkmark on the selected row it adds an additional checkmark. Here's my code

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

Any help would be very much appreciated.

任何帮助将不胜感激。

- (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;

    }


}

回答by Naveen Prasad R

[self.tableView cellForRowAtIndexPath:indexPath]call in the didSelectRowAtIndexPathwill not return the exact cell. It can be same cell, new cell or reused cell. If it is a reused cell at its accessory view has a checkmark, you will end up having two cells with checkmark.

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

Its better to store in the array and use it accordingly. If you are planning to have multiple selections, Use the code example below.

最好存储在数组中并相应地使用它。如果您打算进行多项选择,请使用下面的代码示例。

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

回答by Neru

Try this:

尝试这个:

Declare in your .m file:

在您的 .m 文件中声明:

@interface MyViewController () {
        NSIndexPath *__selectedPath;
    }

In tableView:cellForRowAtIndexPath:check if given indexPath is the same as stored in ivar:

tableView:cellForRowAtIndexPath:检查如果给indexPath是与存储在伊娃:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {       
    //configure cell

    if ([__selectedPath isEqual:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

In tableView:didSelectRowAtIndexPathstore pointer to selected NSIndexPath or nil if cell has been deselect

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

I wrote it without checking in Xcode, so there could be some typos, but I show main idea. In my opinion you shouldn't call [self checkMark];in your tableView:cellForRowAtIndexPath:method.

我写的时候没有检查 Xcode,所以可能会有一些错别字,但我展示了主要思想。在我看来,你不应该调用[self checkMark];你的tableView:cellForRowAtIndexPath:方法。

Moreover if you want to have only one selected cell at a time, you should't create NSMutableArrayto store NSIndexPath. It seems yours cellSelectedstores 2 NSIndexPathsat a time, that why you have this strange behaviour.

此外,如果您只想一次选择一个单元格,则不应创建NSMutableArray以存储NSIndexPath. 似乎你一次cellSelected存储 2NSIndexPaths个,这就是为什么你有这种奇怪的行为。