我正在为我的 iPhone 项目使用 StoryBoard 功能。在 Storyboard中,我有一个 View Controller ,其中包含使用原型(prototype)单元的 TableView 。在原型(prototype)单元内部,我有一个 UIButton(圆角矩形)连接到子类 UIButton 类。 tableview从数组中获取数据,并将数据打印在cellForRowAtIndexPath中。
在 cellForRowAtIndexPath 内,我有条件地修改单元格的外观。这是我开始遇到问题的时候。我能够成功更改按钮的文本。但是,如果我尝试更改按钮的图像,请使用:
[cell.myButton setImage:[UIImage imageNamed:@"mynewimage.png"] forState:UIControlStateNormal];
图像发生变化,但文字消失。
如果我尝试更改按钮的文本颜色,请使用:
[cell.myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal]
什么也没发生。颜色保持不变(除非我也尝试更改图像,在这种情况下它会完全消失)。
我感觉我在这里做了一些根本错误的事情——你有什么建议吗?
最佳答案
使用
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
更改图像并保留文本
使用
btn.titleLabel.textColor = yourcolor;
更改文本颜色
关于iphone - UIButton setTitleColor 和 setImage 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479977/