简体   繁体   English

在情节提要的原型单元中更改UIButton状态

[英]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. 一个用于IBAction,另一个用于IBOutlet。 Then subclass the UITableViewCell to MyTableViewCell and point it to Prototype Cell in storyboard. 然后将UITableViewCell子类化为MyTableViewCell并将其指向情节提要中的Prototype Cell。 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. 然后,我将动作按钮控制+拖动到MyTableViewCell以创建IBAction,然后对插座按钮执行相同的操作以创建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 原因:'-[UITableViewCellScrollView outletButton]:无法识别的选择器已发送到实例

What I am doing wrong? 我做错了什么? What I try to do is to have two buttons in Prototype Cell. 我想做的是在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. 在iOS6和更早的版本上,您的代码看起来不错,但在iOS 7上,如果要进入单元格,则必须再进行一次超级superview 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. 正如您所发现的那样,通常不建议以这种方式浏览视图层次结构,因为不同版本的OS可能会破坏代码。

If your IBAction and IBOutlet are both connected to your MyTableViewCell class, you can simply use: 如果您的IBAction和IBOutlet都连接到MyTableViewCell类,则可以简单地使用:

- (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. 您还需要在MyTableViewCell中实现prepareForReuse: :,以在重用单元格时停止复制按钮的属性。

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

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