【问题标题】:Multiple checkMark when row selected in UITableView IOSUITableView IOS中选择行时的多个复选标记
【发布时间】:2023-04-04 08:08:01
【问题描述】:

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

非常感谢任何帮助。

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

    }


}

【问题讨论】:

    标签: ios objective-c uitableview didselectrowatindexpath


    【解决方案1】:

    试试这个:

    在您的 .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 的指针,如果单元格已被取消选择,则为 nil

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

    我在没有检查 Xcode 的情况下编写了它,所以可能会有一些拼写错误,但我展示了主要思想。 在我看来,您不应该在您的 tableView:cellForRowAtIndexPath: 方法中调用 [self checkMark];

    此外,如果您希望一次只选择一个单元格,则不应创建NSMutableArray 来存储NSIndexPath。看来你的 cellSelected 一次存储 2 个 NSIndexPaths,这就是你有这种奇怪行为的原因。

    【讨论】:

    • 非常感谢,但我需要调用 checkMark 方法,因为我有一个带有 indexPath 的 MutableArray,具体取决于所选的单元格,并且我需要那些带有复选标记的单元格。我是新手,希望你能帮助我。
    • 如果您只想一次选择一个单元格,请不要使用 NSMutableArray。只是 NSIndexPath。 NSMutableArray 在内存中占用更多空间,因此这里不需要使用它。此外,在使用 NSIndexPath 时,您将确保只存储一个指向 NSIndexPath 的指针,并且在 tableView 重新加载数据期间,只会选择一个或零个 UITableViewCell。
    【解决方案2】:

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

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

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

    【讨论】:

    • 为什么需要 [tableView deselectRowAtIndexPath:indexPath animated:YES]?当我评论这一行时,它也很好用。
    • 我遇到了同样的问题,但是对于图像,我为每个单元格都有一个图像,当我选择一行时,我需要更改所选行的图像,但是当我改变这个时图像,还有另一个图像变化也许你可以帮助我
    • 取消选择是删除触摸单元格时突出显示的行为。
    • Klinkert0728 需要查看您的代码以找到您面临的实际问题。
    • 与其重新加载整个tableView,不如用“reloadRowsAtIndexPaths”重新加载选中的行不是更好吗?
    猜你喜欢
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多