繁体   English   中英

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

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

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

谁能告诉我该怎么做?

提前致谢....

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

单选

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

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

暂无
暂无

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

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