遵守<UIPickerViewDataSource, UIPickerViewDelegate>
//初始化
- (UIPickerView *)pickerView
{
if (_pickerView == nil) {
//初始化一个pickerView
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
//设置背景色
//设置代理
_pickerView.delegate = self;
_pickerView.dataSource = self;
//默认显示当前的下标
// [_pickerView selectRow:3 inComponent:0 animated:NO];
}
return _pickerView;
}
//返回文字
- (NSString *)getCurrentSelectedInfo
{
return string;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setupUI];
}
return self;
}
- (void)setupUI
{
// 加载数据
arrayData = [NSMutableArray arrayWithObjects:@"请选择性别",@"男",@"女", nil];
[self addSubview: self.pickerView];
}
//返回有几列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//几行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return arrayData.count;
}
//返回指定列,行的高度,就是自定义行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 40.0f;
}
//返回指定列的宽度
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
if (component==0) {//iOS6边框占10+10
return self.frame.size.width/2;
} else if(component==1){
return self.frame.size.width/2;
}
return self.frame.size.width/2;
}
//显示的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [arrayData objectAtIndex:row];
}
//设置显示的Laber
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
//设置分割线的颜色
for(UIView *singleLine in pickerView.subviews)
{
if (singleLine.frame.size.height < 1)
{
singleLine.backgroundColor = colorText;
}
}
UILabel *genderLabel = [UILabel new];
genderLabel.textAlignment = NSTextAlignmentCenter;
genderLabel.text = arrayData[row];
genderLabel.adjustsFontSizeToFitWidth = YES;
if (selectedRow == row) {
genderLabel.textColor = colorButton;
}else{
genderLabel.textColor = colorText;
}
return genderLabel;
}
//被选择的行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
selectedRow = row;
[self.pickerView reloadAllComponents];//重点
string =[arrayData objectAtIndex:row];
}
UIPickerView
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、UIPickerView(拾取器)的使用 1、UIPickerView控件生成的表格可以提供滚动的轮盘 ...