无法更改所选自定义单元格中的图像 [英] Can't change image in selected custom cell

查看:26
本文介绍了无法更改所选自定义单元格中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[tableView deselectRowAtIndexPath:indexPath 动画: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 是一个 UIImageView

我错过了什么吗?

解决方案

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

- (void)setSelected:(BOOL)selected animation:(BOOL)animated{[super setSelected:selectedanimated:animated];//为选中状态配置视图如果(选择){self.check.image = [UIImage imageNamed:@"1355327732_checkbox-checked"];} 别的 {self.check.image = nil;}}

那么你不必在 tableView 代码中做任何事情来改变它.它会起作用.

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

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

self.tableView.allowsMultipleSelection = YES;

这将设置它以便您可以选择多行.轻按一次选择,另一次轻按取消选择.

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

NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];

I created a custom cell to display a text and 2 images, when the user selects the cell, the image is supposed to change. I can access the properties of the cell, but can't change them :

- (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

Am i missing something?

解决方案

If you are using a custom cell then you can override the function setSelected:animated: like so...

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

Then you don't have to do anything in the tableView code to change this. It will just work.

A better alternative to this is to keep the image the same inside self.check. Then you can just set hidden to YES or NO accordingly. This will be more performant also.

To have this so that you get multiple selections from the table then in the TableViewController put...

self.tableView.allowsMultipleSelection = YES;

This will set it so that you can select multiple rows. One tap selects and another tap deselects.

To get the selected rows you can run...

NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];

这篇关于无法更改所选自定义单元格中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆