循环 创建 UIButton 并添加选中状态 (单选和多选)

自己封装一个View

//在.h声明
@interface SelectView : UIView
@property(nonatomic,assign)NSInteger selectIndex;
- (instancetype)initWithTitle:(NSString *)title andItemArrar:(NSArray *)items;
@end
//在.m里面实现
#import "SelectView.h"
#import "Masonry.h"
@interface SelectView()
@property(nonatomic,strong)UILabel * titleLabel;
@property(nonatomic,strong)UIButton * selectBtn;
@property(nonatomic,strong)NSString * title;
@property(nonatomic,strong)NSArray * items;

@end
@implementation SelectView
- (instancetype)initWithTitle:(NSString *)title andItemArrar:(NSArray *)items{
    if (! (self = [super init]))
        return nil;
    self.backgroundColor = [UIColor yellowColor];
    _title = title;
    _items = items;
    [self addSubview:self.titleLabel];
    [self layoutPageFrame];
    [self addSubview:self.selectBtn];
    return self;
}
//点击事件
- (void)selectBtnClick:(UIButton *)sender{
 //多选模式
 //    if (!sender.selected) {
//        [sender setBackgroundColor:[UIColor brownColor]];
//    }else{
//        [sender setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
//    }
//    sender.selected = !sender.selected;
//单选模式
    if (_selectBtn == nil){
        sender.selected = YES;
        _selectBtn = sender;
    }
   else  if (_selectBtn !=nil &&_selectBtn == sender){
       sender.selected = YES;
    } else if (_selectBtn!= sender && _selectBtn!=nil){
        _selectBtn.selected = NO;
        sender.selected = YES;
        _selectBtn = sender;
    }
    _selectIndex = sender.tag;
}
- (void)layoutPageFrame{
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(5);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(360);
    }];
}
- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]init];
         _titleLabel.backgroundColor = [UIColor orangeColor];
        _titleLabel.font = [UIFont systemFontOfSize:15.0];
        _titleLabel.text = _title;
        _titleLabel.textColor = [UIColor blackColor];
    }
    return _titleLabel;
}
- (UIButton *)selectBtn{
    if (_selectBtn == nil) {
        if (_items>0) {
            for (int i = 0;  i <_items.count; i ++) {
                _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                [_selectBtn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
                [_selectBtn setTitle:[_items objectAtIndex:i] forState:UIControlStateNormal];
                 [_selectBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                _selectBtn.backgroundColor = [UIColor whiteColor];
                [_selectBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
                //单选用到的
                [_selectBtn setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
                [_selectBtn setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
                CGFloat tipDescW =95.0;
                CGFloat tipDescH =30.0;
                int totalColumns = 3;//
                CGFloat marginX = (365-totalColumns*tipDescW)/(totalColumns +1);
                int row = i/totalColumns;
                int col = i%totalColumns;
                _selectBtn.frame = CGRectMake(marginX+ col*(marginX + tipDescW), 40+(row* tipDescH)+(row * 20), tipDescW, tipDescH);
                _selectBtn.tag =i;
                [self addSubview:_selectBtn];
                [_selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
    }
    return _selectBtn;
}
@end

用的时候在Controller里面 直接使用就可以了

 NSArray * items = @[@"1人",@"2人",@"3人",@"四人",@"5人"];
    SelectView * selectView = [[SelectView alloc]initWithTitle:@"入住人数" andItemArrar:items];
    CGFloat selectViewH = (items.count/3+1) * 60 + 40;
    selectView.frame = CGRectMake(0, 150, self.view.frame.size.width, selectViewH);
    [self.view addSubview:selectView];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值