简体   繁体   English

无法更改UITableViewCell中按钮的选定状态图像

[英]unable to change selected state image of button in UITableViewCell

I have a button action and outlet linked to my TableViewCell.h. 我有一个按钮动作和插座链接到我的TableViewCell.h。 In storyboard I'm able to set the default state image and I've added a selected state image. 在情节提要中,我可以设置默认状态图像,并添加了选定的状态图像。 The problem is when selected nothing happens. 问题是当选择时什么也没有发生。 In storyboard the action says 'touch up inside'. 在情节提要中,该动作表示“在内部进行修饰”。 When pressed code registers in coredata, but visually nothing is changing. 当按下的代码在coredata中注册时,但在视觉上没有任何变化。

In my TableViewCell.hi have the action, but because of other properties delegate to UIViewController: 在我的TableViewCell.hi中有操作,但是由于其他属性委托给UIViewController:

- (IBAction)myBtn:(id)sender {
    [self.delegate tableViewCell:self];
}

In UIViewController: 在UIViewController中:

-(void)tableViewCell:(TableViewCell *)cell {
// code...
}

Ultimately I'm trying to create a Bool for the button press so it is either in default state or selected, but also giving me troubles. 最终,我试图为按钮按下创建一个布尔型,使其处于默认状态或处于选中状态,但同时也给我带来了麻烦。

I've also tried setting the images in code: 我也尝试过在代码中设置图像:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//code...
    [cell.myBtn setBackgroundImage:[UIImage imageNamed:@"link normal"] forState:UIControlStateNormal];
    [cell.myBtn setBackgroundImage:[UIImage imageNamed:@"link highlight"] forState:UIControlStateHighlighted];

But again on selection nothing happens. 但是再次选择时什么也没有发生。

Update the selected property of button in IBAction 更新IBAction中按钮的选定属性

- (IBAction)myBtn:(id)sender {
    sender.selected = !sender.selected
    [self.delegate tableViewCell:self];
}

Try this: 尝试这个:

[cell.myBtn setBackgroundImage:[UIImage imageNamed:@"link highlight"] forState:UIControlStateSelected];

highlighted stae and selected state are different. 高亮显示的stae和所选状态不同。

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

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