您的位置:首页 > 产品设计 > UI/UE

UIPickerView

2017-02-14 00:00 381 查看
摘要: UIPickerView的使用跟UITableView,UICollectionView的使用相似,可以对比的学习。PS(后台特殊需求,做个城市地区选择器,必须调接口一层一层的解析,无语。。。。。。。)

// 老套路三步走: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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UIPickerView