首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在UITableView IOS中选中行时有多个checkMark

在UITableView IOS中选中行时有多个checkMark
EN

Stack Overflow用户
提问于 2014-05-19 05:54:00
回答 2查看 24.2K关注 0票数 15

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

任何帮助都将不胜感激。

代码语言:javascript
运行
复制
- (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;

    }


}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-19 06:41:46

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

最好是存储在数组中,并相应地使用它。如果您计划有多个选择,请使用下面的代码示例。

代码语言:javascript
运行
复制
- (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];
}
票数 41
EN

Stack Overflow用户

发布于 2014-05-19 06:21:52

试试这个:

在.m文件中声明:

代码语言:javascript
运行
复制
@interface MyViewController () {
        NSIndexPath *__selectedPath;
    }

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

代码语言:javascript
运行
复制
- (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的指针

代码语言:javascript
运行
复制
- (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,这就是为什么你会有这种奇怪的行为。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23727255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档