ios - 无法更改所选自定义单元格中的图像

标签 ios uiimage tableview custom-cell

我创建了一个自定义单元格来显示一个文本和 2 个图像,当用户选择该单元格时,图像应该会改变。我可以访问单元格的属性,但不能更改它们:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    CustomCell *cell = (CustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    cell.check.image = setImage:[UIImage imageNamed:@"1355327732_checkbox-checked"];
    [cell.check setImage:[UIImage imageNamed:@"1355327732_checkbox-checked"]]; }

cell.check is a UIImageView

我错过了什么吗?

最佳答案

如果您使用的是自定义单元格,那么您可以像这样覆盖函数 setSelected:animated: ...

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    if (selected) {
        self.check.image = [UIImage imageNamed:@"1355327732_checkbox-checked"];
    } else {
        self.check.image = nil;
    }
}

那么您不必在 tableView 代码中做任何事情来改变它。它会正常工作。

一个更好的替代方法是在 self.check 中保持图像相同。然后你可以相应地将 hidden 设置为 YES 或 NO。这也会提高性能。

为了让您从表中获得多个选择,然后在 TableViewController 中放置...

self.tableView.allowsMultipleSelection = YES;

这将设置它以便您可以选择多行。一次点击选择,另一次点击取消选择。

要获取选定的行,您可以运行...

NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];

关于ios - 无法更改所选自定义单元格中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13844724/

相关文章:

ios - 如何实现图像组?

ios - 点击标签栏时将表格 View 滚动到顶部

iphone - 为什么 applicationWillTerminate : delegate does not fire on minimize?

ios - 从字典中删除嵌套键

ios - 在给定时间捕获 AVPlayer 流 .m3u8 的 UIImage 帧?

ios - 将图像添加到ios工具栏的UIBarButtonItem

iphone - iOS 中的自动图像方向

JavaFX 捆绑 exe 不显示 TableView 内容

ios - 在 swift 2.0 中允许用户播放背景音乐

ios - @Selector 什么都没发生