简体   繁体   English

setImage:forState:用于自定义UITableViewCell的UIButton

[英]setImage:forState: for UIButton for a custom UITableViewCell

In my nib file, I have an outlet connected a UIButton for the UITableViewCell. 在我的笔尖文件中,我有一个插座连接了UITableViewCell的UIButton。 Where can I set the image for the different states of the button in code? 在哪里可以为代码中按钮的不同状态设置图像?

By default, there is a method 默认情况下,有一种方法

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier    {

}

I tried setting my image for UIControlStateNormal and UIControlStateSelected, but I don't see my states when I load the TableViewCell. 我尝试为UIControlStateNormal和UIControlStateSelected设置图像,但是加载TableViewCell时看不到状态。 I load the TBvCell using the UINib method: 我使用UINib方法加载TBvCell:

MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"MyCellIdentifier"];
     if (cell == nil) {
     UINib *cellNib = [UINib nibWithName:@"MyCell" bundle:nil];
     [cellNib instaniateWithOwner:self options:nil];
     cell = self.MyCell;
     self.MyCell = nil;
}
return cell;

Thanks. 谢谢。

You can set all this in Interface Builder itself. 您可以在Interface Builder本身中进行所有设置。 Unless you have very string reasons to do it in code. 除非您有非常字符串的理由要在代码中执行此操作。 Here's how to do it in IB - 这是在IB中的操作方法-

Open the right side bar & then click on "State Config", there you see the various states of the button, Default, Highlighted, Selected & Disabled. 打开右侧栏,然后单击“状态配置”,您会看到按钮的各种状态,默认,突出显示,选定和禁用。 Now you can set different images for each state, different font type & font colors for each state. 现在,您可以为每个州设置不同的图像,为每个州设置不同的字体类型和字体颜色。 Hopr this helps... 霍普这有帮助...

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM