简体   繁体   English

选择tableView单元格时更改单元格上的按钮图像

[英]Change button image on a cell when a tableView Cell is selected

I'm having a UITableView with the table view cell loaded from an .xib. 我有一个UITableView与从.xib加载的表视图单元格。 Which has a label text and a checkmark button. 其中有一个标签文本和一个选中标记按钮。 I want to change the checkmark image of the button when the cell is selected. 我想在选择单元格时更改按钮的选中标记图像。 (Allows multiple selection). (允许多项选择)。 From these selected cells with changed checkmark image, i'm performing an operation. 从这些选定的带有更改的选中标记图像的单元格中,我正在执行一项操作。

Can anyone tell me how this could be done? 谁能告诉我该怎么做?

Thanks in advance.... 提前致谢....

Follow the below to change the button image. 请按照以下步骤更改按钮图像。

Single Selection 单选

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    if (selectedIndex == indexPath.row) {
      // change image here...
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selectedIndex = indexPath.row;
    [tableView reloadData];
}

If you need for multiple selection then you can maintain a array to capture the indexes and do the needful like below. 如果需要多项选择,则可以维护一个数组以捕获索引并执行如下所示的有需要的操作。

Multiple selection 多项选择

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   if ([selectedIndexArray containsObject:indexPath.row]) {
      // change image to check mark here...
   } else {
      // Change image to un-check mark
   }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([selectedIndexArray containsObject:indexPath.row]) {
       [selectedIndexArray removeObject:indexPath.row];
    } else {
       [selectedIndexArray addObject:indexPath.row];
    }
  [tableView reloadData];
}

You can have a field isCheckMarked in your model class, which you have then put in array for your tableview to populate. 您可以在模型类中具有一个isCheckMarked字段,然后将其放入数组中以供表视图填充。 You can then toggle that flag in didSelectRow delegate of tableView and can change checkmark image based on isCheckMarked 然后,您可以在tableView的didSelectRow委托中切换该标志,并可以基于isCheckMarked更改选中标记图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM