iOS:一个Cell中设置另外一个Cell中的button

场景:

子类化Cell中有button,拥有选中式样,点击第一个Cell中的button后,Cell一中的button获得选中式样。可是当点击Cell二中的button时。Cell一中的button选中式样依旧存在,即同一时候存在两个选中的button。
解决方法:

在子类化的Cell中,定义一个

static UIButton *selectedButton;

必须是 static 静态变量。

然后在点击方法中。

 - (void)headerButtonClickAction:(UIButton *)btn
  {
      selectedButton.layer.borderWidth = 0;
      btn.layer.borderWidth = 2;
      selectedButton = btn;
  }

这种话我就能取到上次点击的button,

        我设置的是边框宽度显示是否选中。自己能够依据自己的需求写demo。


这里要不用操心第一次运行时selectedButton为nil,为nil时。不会对程序产生影响。

posted @ 2017-04-23 09:35  claireyuancy  阅读(194)  评论(0编辑  收藏  举报