【问题标题】:How to toggle UITableView Cell selected status如何切换 UITableView 单元格选择状态
【发布时间】:2025-12-06 00:50:01
【问题描述】:

我有一个带有自定义单元格的 UITableView,该单元格包含一个 UIImageView 和一个 UILabel。现在,当我第一次加载表格时,它会在每个单元格上加载相同的图像和不同的标签,这些标签来自 LabelArray。

现在我说的图像是一个单选按钮,所以当用户点击单元格时,图像会发生变化。如果用户再次单击它会更改为默认状态。

为此,我使用了这个函数,并且在我的 customcell 类中声明了一个 bool 变量,称为 selectionStatus。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell * cell = (CustomCell* )[tableView cellForRowAtIndexPath:indexPath];

    if(indexPath.row == 0)
    {
        if(cell.selectionStatus == TRUE)
        {           
            //Do your stuff
           cell.selectionStatus = FALSE;
        }
        else
        {
            //Do your stuff
            cell.selectionStatus = TRUE;
        }
    }

    if(indexPath.row == 1)
    {
        if(cell.selectionStatus == TRUE)
        {           
            //Do your stuff
           cell.selectionStatus = FALSE;
        }
        else
        {
            //Do your stuff
            cell.selectionStatus = TRUE;
        }
    }
}

这很好用,(但我想知道它是否是正确的方法,或者我们可以检查 cell.selected 属性)并且我能够获得这种效果。但是现在当我关闭视图并再次打开它时,功能

使用@Anil 根据下面的 cmets 进行编辑

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

    if([self.checkedIndexpath count]  == 0)
    {
        [tableCell.selectionImage setImage:@"xyz.png"];
    }
    else
    {        
        for (int i =0; i <[self.checkedIndexPath count]; i++)
        {
            NSIndexPath *path = [self.checkedIndexPath objectAtIndex:i];
            if ([path isEqual:indexPath])
            {               
                [tableCell.selectionImage setImage:@"abc.png"]
            }
            else
            {
                 [tableCell.selectionImage setImage:@"xyz.png"]            
             }
    }
return tableCell;

问候 兰吉特

【问题讨论】:

  • 我建议至少使用 [tableView deselectRowAtIndexPath:indexPath animated:YES];取消选择您的单元格。
  • 和 [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];选择您的单元格。为您提供用于选择和取消选择单元格的默认 IOS 动画。
  • 嗨@shoughton123,谢谢你的回复,但是当表格再次加载时呢
  • 只需将选择的索引保存为变量,当重新加载表时,只需使用这些方法重新选择相同的索引。

标签: ios ios5 uitableview ios6


【解决方案1】:

切换 UItableViewCells 的另一个选项...

如果单元格已被选中,在 willSelectRowAtIndexPath 中为 NSIndexPath 返回 nil,同时设置您的 _selectedIndexPath 实例变量。

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    if (cell.selected) {
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        [self.tableView.delegate tableView:self.tableView didDeselectRowAtIndexPath:indexPath];
        indexPath = nil;
    }

    _selectedIndexPath = indexPath;

    return indexPath; 
}

在 didSelectRowAtIndexPath 和 didDeselectRowAtIndexPath 中根据属性 cell.selected 更新您的单元格 ...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell.selected) {
        // do stuff
    } else {
        // do stuff
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell.selected) {
        // do stuff
    } else {
        // do stuff
    }; 
}

最后在 viewDidLoad 中设置 clearsSelectionOnViewWillAppear 属性...

- (void)viewDidLoad {
    [super viewDidLoad];
    self.clearsSelectionOnViewWillAppear = NO;
}

【讨论】:

    【解决方案2】:

    你要在didSelectRowAtIndexPath中保存选中行的索引路径,并在cellForRowAtIndexPath中查看索引路径:设置对应的图片

    您要多选吗?试试这个...

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    CustomCell * cell = (CustomCell* )[tableView cellForRowAtIndexPath:indexPath];
    
    if(indexPath.row == 0)
    {
        if(cell.selectionStatus == YES)
        {     
          [self.checkedIndexPaths addObject:indexPath];
            //Do your stuff
           cell.selectionStatus = NO;
        }
        else
        {
            [self.checkedIndexPaths removeObject:indexPath];
            //Do your stuff
            cell.selectionStatus = YES;
        }
    }
    }  
    

    编辑
    在 cellForIndexPath 中像这样检查

     // Set the default image for the cell. imageXYZ   
    for (NSIndexPath *path in self.checkedIndexPath) 
    {
        if ([path  isEqual:indexPath])
        {
            //set the changed image for the cell. imageABC
        }
        // no need of else part
    }
    

    按照我们会看到的那样做

    【讨论】:

    • 好吧,我将indexpath保存在一个数组中,但是用户可以点击它100次,然后每次用户点击它都会被添加到数组中。
    • 嗨@Anil,感谢您的编辑,如何在 CellForRowAtIndexPath 中检查它
    • 嗨@Anil,虽然他们是一个问题,但如果我选择 3 行,发生的事情是它只显示选择的第三行。如果我选​​择 4 行,那么它会显示我只有第 4 个被选中,之前的被取消选中。
    • checkedIndexPath 数组只包含一个 indexPath??检查...我没有尝试过...这是我的逻辑b:)
    • 尝试打印所有的checkedIndexPath.row,同时在cellForRow中枚举以确保每个都是不同的
    【解决方案3】:

    在模型类中获取一个布尔值并在 didSelectRow 方法上更新它并重新加载表。在 cellForRowAtIndexPath 中这样检查。

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     After making your cell check like that 
    
        ModelClass *yourModel=[self.yourArray objectAtIndex:indexPath.row];
       if(yourModel.selection)
         [cell.customImage setImage:[UIImage imageName:@"selected"]; 
      else
         [cell.customImage setImage:[UIImage imageName:@"un_selected"];
    

    }

    检查此链接 https://github.com/adnanAhmad786/Quiz-View--TableView

    【讨论】:

      【解决方案4】:
      1. 首先为了切换目的,在 tableview 单元格类中声明一个 bool 值 selectionStatus,如下所示:

        @property(nonatomic)BOOL selectionStatus;

      2. 声明一个 NSMutableArray 来存储选定的索引路径(用于多选)

        self.checkedIndexPaths = [[NSMutableArray alloc] init];

      3. 在 didSelectRowAtIndexPath 中

        一个。如果布尔值 selectionStatus 为 No 则将 indexPath 插入 NSMutableArray checkedIndexPaths 并设置 cell.selectionStatus = YES

        b.如果布尔值 selectionStatus 为 Yes 从 NSMutableArray 中删除 indexPath 并设置 cell.selectionStatus = NO

        c。每次重新加载tableView

      找到下面的代码:

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
      
          tableCell = [self.tableView cellForRowAtIndexPath:indexPath];
      
      
              if(cell.selectionStatus == YES)
              {
                  [self.checkedIndexPaths removeObject: indexPath];
                  cell.selectionStatus = NO;
      
              }
              else
              {
      
                  [self.checkedIndexPaths addObject: indexPath];
                  cell.selectionStatus = YES;
      
      
              }
      
          [self.tablee reloadData];
      }
      
      1. 在 cellForRowAtIndexPath 中

        一个。首先将单元格图像设置为未选中,即此处的 xyz.png

        [tableCell.selectionImage setImage:[UIImage imageNamed:@"xyz.png"]];

        b.将图像设置为检查 NSMutable 数组 self.checkedIndexPaths 中的所有索引路径

        for (NSIndexPath *path in self.checkedIndexPaths)
        {
            if ([path  isEqual:indexPath])
            {
                [tableCell.selectionImage setImage:[UIImage imageNamed:@"abc.png"]];
            }
        
        }
        

      【讨论】:

        最近更新 更多