iOS学习 - UIPickerView

UIPickerView使用:




效果所示

1、单个PickerView的使用



#pragma mark ===== UIPickerView

-(UIPickerView *)pickerView

{

if (!_pickerView) {

_pickerView=[[UIPickerView alloc]init];

_pickerView.frame =CGRectMake(0, YHScreenHeight-300, YHScreenWith, 300);

_pickerView.backgroundColor=[UIColor whiteColor];

_pickerView.delegate = self;

_pickerView.dataSource = self;

_pickerView.showsSelectionIndicator = YES;

}

return _pickerView;

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 1;

}

#pragma mark =====

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

return self.ProvinceArray.count;

}

#pragma mark ===== 显示内容

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

return self.ProvinceArray[row];

}

#pragma mark ===== 每列宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {

return YHScreenWith;

}

#pragma mark ===== 行高

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

{

return 40;

}

#pragma mark ===== 返回选中的行

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

NSString *str=self.ProvinceArray[row]; //输出的结果

}

#pragma mark ===== 自定义显示的view

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

UILabel* pickerLabel = (UILabel*)view;

if (!pickerLabel){

pickerLabel = [[UILabel alloc] init];

pickerLabel.adjustsFontSizeToFitWidth = YES;

pickerLabel.textColor=[UIColor redColor];

pickerLabel.textAlignment = NSTextAlignmentCenter;

[pickerLabel setBackgroundColor:[UIColor clearColor]];

[pickerLabel setFont:[UIFont boldSystemFontOfSize:18]];

}

//设置分割线的颜色

for(UIView *singleLine in pickerView.subviews)

{

if (singleLine.frame.size.height < 1)

{

singleLine.backgroundColor = [UIColor greenColor];

}

}

pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];

return pickerLabel;

}

iOS学习 - UIPickerView_第1张图片
效果图




2 ,多个PickerView的使用


#pragma mark ===== UIPickerView

-(UIPickerView *)pickerView

{

if (!_pickerView) {

_pickerView=[[UIPickerView alloc]init];

_pickerView.backgroundColor=[UIColor whiteColor];

_pickerView.delegate = self;

_pickerView.dataSource = self;

_pickerView.showsSelectionIndicator = YES;

}

return _pickerView;

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 3;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

NSInteger integer=0;

if (component==0) {

integer=self.ProvinceArray.count;

}

else if (component==1)

{

ProvinceDataModel * model= self.ProvinceArray[self.selectRowWithProvince];

integer=model.city.count;

}

else if (component==2)

{

ProvinceDataModel * model= self.ProvinceArray[self.selectRowWithProvince];

CityDataModel *cityModel=model.city[self.selectRowWithCity];

integer=cityModel.District.count;

}

return integer;

}

#pragma mark ===== 显示内容

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

NSString *string =nil;

if (component==0) {

ProvinceDataModel * model= self.ProvinceArray[row];

string=model.name;

}

else if (component==1)

{

ProvinceDataModel * model= self.ProvinceArray[self.selectRowWithProvince];

CityDataModel *cityModel=model.city[row];

string=cityModel.name;

}

else if (component==2)

{

ProvinceDataModel * model= self.ProvinceArray[self.selectRowWithProvince];

CityDataModel *cityModel=model.city[self.selectRowWithCity];

DistrictDataModel *TeanModel=cityModel.District[row];

string=TeanModel.name;

}

return string;

}

#pragma mark ===== 每列宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {

return YHScreenWith/3;

}

#pragma mark ===== 行高

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

{

return 40;

}

#pragma mark ===== 返回选中的行

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

if (component==0)

{

self.selectRowWithProvince=row;

self.selectRowWithCity=0;

[pickerView reloadComponent:1];

[pickerView selectRow:0 inComponent:1 animated:YES];

[pickerView reloadComponent:2];

[pickerView selectRow:0 inComponent:2 animated:YES];

}

else if (component==1)

{

self.selectRowWithCity=row;

[pickerView reloadComponent:2];

[pickerView selectRow:0 inComponent:2 animated:YES];

}

else

{

self.selectRowWithTown=row;

}

}

#pragma mark ===== 自定义显示的view

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

UILabel* pickerLabel = (UILabel*)view;

if (!pickerLabel){

pickerLabel = [[UILabel alloc] init];

pickerLabel.adjustsFontSizeToFitWidth = YES;

pickerLabel.textColor=[UIColor redColor];

pickerLabel.textAlignment = NSTextAlignmentCenter;

[pickerLabel setBackgroundColor:[UIColor clearColor]];

[pickerLabel setFont:[UIFont boldSystemFontOfSize:14]];

}

//设置分割线的颜色

for(UIView *singleLine in pickerView.subviews)

{

if (singleLine.frame.size.height < 1)

{

singleLine.backgroundColor = [UIColor magentaColor];

}

}

pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];

return pickerLabel;


iOS学习 - UIPickerView_第2张图片
效果图


Dome 地址: 下载地址

你可能感兴趣的:(iOS学习 - UIPickerView)