更改单元格中的UIButton图像
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I am having difficulty in changing the image of my UIButton which is contained within a table view cell. My code as follows:

 // In tableview for cell at rowIndex method
 self.iconBtn = [[UIButton alloc]initWithFrame:CGRectMake(670,10,80, 80)];
            self.iconBtn.tag = kIconValueTag;

 [cell.contentView addSubview:self.iconBtn];

 //Add icon to cell
 UIImage *btnImage = [UIImage imageNamed:@"blank_star.png"];

 [self.iconBtn setImage:btnImage forState:UIControlStateNormal];

 // I am calling method changeIconState after user clicks on icon.
 [self.iconBtn addTarget:self action:@selector(changeIconState) forControlEvents:UIControlEventTouchUpInside];

After the button is pressed, a method outside the table view method is called:

-(void)changeIconState
{
    if (self.iconSelectState == kIconNotSelected) 
    {
        self.iconSelectState = kIconSelected;
    }
    else
    {
        self.iconSelectState = kIconNotSelected;
    }
    [self changeIcon];

}
-(void)changeIcon
{
    if (self.iconSelectState == kIconSelected) 
    {
        UIImage *btnImageHighlighted = [UIImage imageNamed:@"star.png"];
        [self.iconBtn setImage:btnImageHighlighted forState:UIControlStateNormal];
    }
    else
    {
        UIImage *btnImageNormal = [UIImage imageNamed:@"blank_star.png"];
        [self.iconBtn setImage:btnImageNormal forState:UIControlStateNormal];
    }

}

After I run the program, the icon did not change from blank to star as I wanted. It just remained as a blank star. Is there anything I am missing out here?

#0

Why are you managing the image/button state yourself? Why not load the two images for the defined states and let the OS handle it?

 //Add icon to cell
 UIImage *btnImage = [UIImage imageNamed:@"blank_star.png"];
 UIImage *btnImageHighlighted = [UIImage imageNamed:@"star.png"];

 [self.iconBtn setImage:btnImage forState:UIControlStateNormal];
 [self.iconBtn setImage:btnImageHighlighted forState:(UIControlStateHighlighted && UIControlStateSelected)];

If you need to manage your property (iconSelectedState) you can without worrying about the image. Alternatively, you can query the buttons state property to determine which state it is in.

推荐文章

福勒的“企业应用程序架构模式”是否仍然相关?

福勒的“企业应用程序架构模式”是否仍然相关?

推荐文章

Redis介绍与安装

Redis介绍与安装

推荐文章

zabbix_mongodb监控

zabbix_mongodb监控

推荐文章

撤消Git中一个文件的工作副本修改?

撤消Git中一个文件的工作副本修改?

推荐文章

Redis 3.0 Windows 安装步骤

Redis 3.0 Windows 安装步骤

推荐文章

如何使用JSON框架和Objective-C/iPhone/Xcode解析嵌套的JSON对象?

如何使用JSON框架和Objective-C/iPhone/Xcode解析嵌套的JSON对象?

推荐文章

Redis 登陆

Redis 登陆

推荐文章

postgresql 提升主库的操作

postgresql 提升主库的操作

推荐文章

在VB.NET 2005或C#.net2005中使用多个数据读取器,MySQL 5.0作为后端

在VB.NET 2005或C#.net2005中使用多个数据读取器,MySQL 5.0作为后端

推荐文章

如何在Python中跟踪动画对象?

如何在Python中跟踪动画对象?

推荐文章

[Redis] redis-cli 命令总结

[Redis] redis-cli 命令总结

推荐文章

Rails应用程序管理部分

Rails应用程序管理部分

推荐文章

线上实战zabbix监控mongodb详细步骤

线上实战zabbix监控mongodb详细步骤

推荐文章

System.IO.Stream可与读卡器一起使用

System.IO.Stream可与读卡器一起使用

推荐文章

如何抓取饲料

如何抓取饲料

推荐文章

变形验证码的想法

变形验证码的想法