XZ_iOS中只选中一个button

来源:互联网 发布:数控编程圆弧怎么编程 编辑:程序博客网 时间:2024/05/29 19:25

有的时候tableView的每一行cell的右侧都是一个button,但是我们需要实现的功能是其中一个为选中状态,其他按钮选中状态为不选中状态,这时候我们可以设置一个中间的成员变量CurrentBtn,用来记录当前是哪一个button,用它来进行操作,代码如下:

// 当前点击的button
@property (nonatomic, strong) UIButton *currentBtn;

// 只有一个button能点
        if (self.currentBtn != button) {
            self.currentBtn.selected = !self.currentBtn.selected;
            self.currentBtn = button;
            button.selected = YES;
        }else {
            self.currentBtn.selected = !self.currentBtn.selected;
        }

 // 让tableView默认选中第一行
     NSIndexPath * path =  [NSIndexPath indexPathForRow:0 inSection:0];
     [self tableView:self.tableView didSelectRowAtIndexPath:path];

0 0