简体   繁体   中英

Change UIButton state in Prototype Cell in Storyboard

I have two buttons in Prototype Cell in storyboard. One for IBAction and the other for IBOutlet. Then subclass the UITableViewCell to MyTableViewCell and point it to Prototype Cell in storyboard. Then I control+drag the action button to MyTableViewCell to create a IBAction and do the same for the outlet button to create a IBOutlet.

In the method:

  -(void)buttonPress:(UIButton *)sender
  {
      MyTableViewCell *cell = (MyTableViewCell *)[[sender superview] superview];
      cell.outletButton.selected = YES;
      ...
  }

I get the error message:

reason: '-[UITableViewCellScrollView outletButton]: unrecognized selector sent to instance

What I am doing wrong? What I try to do is to have two buttons in Prototype Cell. When one button is pressed, the other button can change the state to show background images, such as selected or highlighted.

Thanks in advance.

On iOS6 and earlier your code seems fine, but on iOS 7 you have to do one more step of superview if you want to get to the cell. So it will be:

MyTableViewCell *cell = (MyTableViewCell *)[[[sender superview] superview] superview];

It's usually not a good idea to navigate the view hierarchy in this way, as you've found, there can be differences between OS versions that can break your code.

If your IBAction and IBOutlet are both connected to your MyTableViewCell class, you can simply use:

- (void)buttonPress:(UIButton *)sender
{
  self.outletButton.selected = YES;
  ...
}

You will also want to implement prepareForReuse: in MyTableViewCell to stop the properties of the buttons becoming copied when the cell is reused.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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