循环 创建 UIButton 并添加选中状态 (单选和多选)
来源:互联网 发布:域名污染 编辑:程序博客网 时间:2024/05/21 17:23
自己封装一个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
- 循环 创建 UIButton 并添加选中状态 (单选和多选)
- uibutton的单选和多选
- javascript 判断radio单选按钮是否为选中状态,并弹出选中值
- 解决UIButton 选中状态和未选中状态色差问题
- table(单击行,把当行的单选按钮(radio)设为选中状态,并应用当前样式)
- JS 判断 Radio 单选按钮是否为选中状态 并弹出 值信息
- JS 判断 Radio 单选按钮是否为选中状态 并弹出 值信息
- 关于uibutton选中状态和高亮状态的区别
- Android 使用AlertDialog实现多选并保存选中状态
- 表格变色(单击行,把当行的单选按钮(radio)设为选中状态,并应用当前样式)
- UIButton循环创建
- 怎么判断单选 复选框的选中状态
- 单选UIbutton的实现
- 创建多个UIButton其中只有一个显示选中效果
- jQuery获取单选按钮radio选中值与去除所有radio选中状态
- ios中UIButton选中状态切换
- UIButton选中状态下的点击
- UIButton选中状态下的点击
- 不同阶层数据分析师都在做什么呢
- <iOS>关于viewWithTag的一点说明
- java通过给按钮添加监听器理解接口的作用
- webService之(一)java原生态服务端
- Activity dispatchTouchEvent事件分发--总结(一)
- 循环 创建 UIButton 并添加选中状态 (单选和多选)
- CUDA 实现JPEG图像解码为RGB数据
- write excel with python xlwt
- UVA 10943 How do you add?(隔板法)
- Oracle表分区合并
- MySQL详解--锁
- PHPthinking邀请您一起赚Money
- 适配器模式,装饰模式,代理模式异同
- uboot的makefile分析