gpt4 book ai didi

objective-c - 更改单元格内的 UIButton 图像

转载 作者:行者123 更新时间:2023-11-29 13:48:19 24 4
gpt4 key购买 nike

我很难更改包含在表格 View 单元格中的 UIButton 的图像。我的代码如下:

 // 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];

按下按钮后,调用table view方法之外的一个方法:

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

}

在我运行程序后,图标并没有像我想要的那样从空白变成星号。它只是一颗空白的星星。我在这里遗漏了什么吗?

最佳答案

为什么要自己管理图像/按钮状态?为什么不为定义的状态加载两个图像并让操作系统处理呢?

 //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)];

如果您需要管理您的属性 (iconSelectedState),您可以不用担心图像。或者,您可以查询按钮状态属性以确定它所处的状态。

关于objective-c - 更改单元格内的 UIButton 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6242542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com