UIPickerView

// 老套路三步走:1,遵守协议;2设置代理;3实现代理方法;

// 遵守:<UIPickerViewDelegate,UIPickerViewDataSource>协议

// <1> 创建UIPickerView,设置代理

    self.cityPickeView = [[UIPickerView alloc]init];
    self.cityPickeView.backgroundColor = [UIColor whiteColor];
    self.cityPickeView.delegate = self;
    self.cityPickeView.dataSource = self;
    [self addSubview:self.cityPickeView];
// 2 实现代理方法

// 设置有几个分区
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
// 设置每个分区有多少行(自己换数据)
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return _provincesArray.count;
    }
    if (component == 1) {
        return self.cityArray.count;
    }
    if (component == 2) {
        return self.countyArray.count;
    }
    return 0;
}
// 行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 50;
}
//设置每行的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        CCYTCityModel *model = _provincesArray[row];
        return  model.name;
    }
    if (component == 1) {
        CCYTCityModel *model = _cityArray[row];
        return model.name;
    }
    if (component == 2) {
        CCYTCityModel *model = _countyArray[row];
        return model.name;
    }
        return @"测试";
}
// 选中某一行触发方法(做三级联动)
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
        // 刷新某一个分区
        [_cityPickeView reloadComponent:1];
        // 刷新整个控件
        [_cityPickeView reloadAllComponents]
}
// 设置显示的一些常用属性(字体,颜色。。。)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont fontWithName:PF_Regular size:15]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    pickerLabel.textAlignment = NSTextAlignmentCenter;
    return pickerLabel;
}

转载于:https://my.oschina.net/iOScoderZhao/blog/838285

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值