【问题标题】:Change button image on a cell when a tableView Cell is selected选择 tableView 单元格时更改单元格上的按钮图像
【发布时间】:2015-11-02 05:47:48
【问题描述】:

我有一个UITableView,其中的表格视图单元格是从 .xib 加载的。 其中有一个标签文本和一个复选标记按钮。 我想在选择单元格时更改按钮的复选标记图像。 (允许多选)。 从这些选中标记图像已更改的选定单元格中,我正在执行一项操作。

谁能告诉我这是怎么做到的?

提前谢谢....

【问题讨论】:

  • 检查我的回答 stackoverflow.com/questions/6946991/…>。希望能帮助到你。快乐编码..

标签: ios objective-c uitableview didselectrowatindexpath


【解决方案1】:

按照以下步骤更改按钮图像。

单选

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

如果您需要多项选择,那么您可以维护一个数组来捕获索引并执行以下操作。

多选

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

【讨论】:

    【解决方案2】:

    您可以在模型类中拥有一个字段isCheckMarked,然后将其放入数组中以供表格视图填充。然后,您可以在 tableView 的 didSelectRow 代表中切换该标志,并可以根据 isCheckMarked 更改复选标记图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      相关资源
      最近更新 更多