iOS UIPickView的使用(二)

==注意:pickview上添加按钮点击时会穿透,导致点击按钮没反应,长用做法是吧pickview和按钮都放到另一一个view上,让按钮和pickview没有父子关系。


UIPickView的使用


// 水果

@property(nonatomic,weak)IBOutletUILabel *fruitLbl;


// 主菜

@property(nonatomic,weak)IBOutletUILabel *mainFoodLbl;


// 酒水

@property(nonatomic,weak)IBOutletUILabel *drinkLbl;


@property (nonatomic,strong)NSArray *foods;


@end


@implementation ViewController




- (void)viewDidLoad {

    [superviewDidLoad];

    

    // 随机色

//    [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0];

    

//    [self pickerView:self.pickerView didSelectRow:0 inComponent:0];

//    [self pickerView:self.pickerView didSelectRow:0 inComponent:1];

//    [self pickerView:self.pickerView didSelectRow:0 inComponent:2];

    

    

    //label启动就显示

    for (int i =0; i <self.foods.count; i++) {

        

        [selfpickerView:self.pickerViewdidSelectRow:0inComponent:i];

    }

}


#pragma mark - 点击随机点餐按钮的时候调用

- (IBAction)randomBtnClick:(UIButton *)sender {

    

    for (int i =0; i <self.foods.count; i++) {

     

        // 1.pickerView随机选中

        // 生成随机数

        // i组对应的范围

        

        // 10  11 0~10

        NSInteger count = [self.foods[i] count];

        u_int32_t randomNumber = arc4random_uniform((int)count);

        

        // 1.2每次的数据都不一样

        // 获取当前列选中的行号

        NSInteger selRow = [self.pickerView selectedRowInComponent:i];

        

        // 如果行号相等,重新生成

        while ((int)selRow == randomNumber) {

            randomNumber = arc4random_uniform((int)count);

        }

        

        [self.pickerView selectRow:randomNumber inComponent:i animated:YES];

        

        

        // 2.label改变

        [self pickerView:self.pickerView didSelectRow:randomNumber inComponent:i];

    }

    

}


#pragma mark - 代理方法

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


    // 1.获取选中的数据

    NSString *selFood = self.foods[component][row];

    

    // 2.设置给label

    if (component == 0) {

        self.fruitLbl.text = selFood;

    } else if (component ==1) {

        self.mainFoodLbl.text = selFood;

    } else {

        self.drinkLbl.text = selFood;

    }

    

}



// 返回每一行显示的内容

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


    // 获取每一组的数据

    NSArray *group = self.foods[component];

    

    // 返回每一行显示的内容

    return group[row];


}



#pragma mark - 数据源方法

// 返回有多少列""

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    

    return self.foods.count;

    

}


// 返回每一组有多少行

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

    

    // 获取每一组的所有数据的数组

    NSArray *group = self.foods[component];

    

    // 返回行数

    return group.count;

}





#pragma mark - 懒加载

- (NSArray *)foods {


    if (_foods == nil) {

        

        // 1.找到文件路径

        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"01foods.plist" ofType:nil];

        

        // 2.转为数组

        _foods = [NSArray arrayWithContentsOfFile:filePath];

        

    }


    return _foods;

    

}




@end

==============================================


#import "ViewController.h"

#import "LBProvince.h"


@interface ViewController () <UIPickerViewDelegate,UIPickerViewDataSource>



@property (weak, nonatomic) IBOutletUIPickerView *pickerView;

@property (weak, nonatomic) IBOutletUILabel *provinceLbl;

@property (weak, nonatomic) IBOutletUILabel *cityLbl;





// 省模型的数组

@property (nonatomic,strong)NSArray *provinces;


// 保存上一次显示或选中的省

@property (nonatomic,strong)LBProvince *selPro;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

 

    // 设置数据源.代理对象

    self.pickerView.dataSource =self;

    self.pickerView.delegate =self;

    

    // 设置默认的选中

    [selfpickerView:self.pickerViewdidSelectRow:0inComponent:0];

}



#pragma mark - 代理方法

//label上显示

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

    

    // 如果滚动的是第0,要刷新第1列城市数据

    if (component == 0) {

        [pickerView reloadComponent:1];

        // 选中第1列第0

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

    }


    // 获取省的名称获取市的名称

    // 得获取pickerView0列的选中行1列的选中行

    NSInteger selProIdx = [pickerView selectedRowInComponent:0];

    NSInteger selCityIdx = [pickerView selectedRowInComponent:1];

    

    // 设置省

    CZProvince *selPro = self.provinces[selProIdx];

    self.provinceLbl.text = selPro.name;

    

    // 设置市

//    self.cityLbl.text = selPro.cities[selCityIdx];

    self.cityLbl.text =self.selPro.cities[selCityIdx];//注意用保存的省得模型去赋值

    

}


// 返回显示的内容

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

    

    NSLog(@"titleForRow");

    

    // 如果是第0,直接返回省的名称

    if (component == 0) {

        CZProvince *pro = self.provinces[row];

        return pro.name;

    } else {

    

//        // 如果是第1,根据省确定市的数据

//        NSInteger selProIdx = [pickerView selectedRowInComponent:0];

//        CZProvince *selPro = self.provinces[selProIdx];//获取省得模型

//        

//        // 城市的数组

//        NSArray *cities = selPro.cities;

//        

//        // 返回内容

//        return cities[row];

        

        return self.selPro.cities[row];

        

    }

}



#pragma mark - 数据源方法

// 返回组的数量

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {


    return 2;

}


// 返回每一组有多少行

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


    // 如果是第0,行数就是模型数量

    if (component == 0) {

        return self.provinces.count;

    } else {


        // 如果是第1,根据第0列的省确定城市的数量

        // 1. 获取第0列选中下标

        NSInteger selProIdx = [pickerView selectedRowInComponent:0];

        

        // 2. 根据下标去获取省模型

        CZProvince *selPro = self.provinces[selProIdx];

        

        

        // 保存选中的省模型

        self.selPro = selPro;

        

        return self.selPro.cities.count;

    

        

//        // 3. 到模型中获取城市数量

//        return selPro.cities.count;

    

    }

}



#pragma mark - 懒加载

- (NSArray *)provinces {


    if (_provinces ==nil) {

        

        // 获取文件路径转为字典数组

        NSArray *dictArr = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"02cities.plist"ofType:nil]];

        

        // 遍历数组转为模型数组

//        for (<#initialization#>; <#condition#>; <#increment#>) {

//            <#statements#>

//        }


//        for (<#type *object#> in <#collection#>) {

//            <#statements#>

//        }

//        for (id obj in dictArr) {

//            NSDictionary *dict = (NSDictionary *)obj;

//            CZProvince *pro = [CZProvince provinceWithDict:dict];

//        }

        

        // 通过block遍历

        // 临时数组保存所有的模型

        NSMutableArray *tempArrM = [NSMutableArrayarrayWithCapacity:dictArr.count];

        [dictArr enumerateObjectsUsingBlock:^(NSDictionary *_Nonnull obj,NSUInteger idx,BOOL *_Nonnull stop) {

            

            CZProvince *province = [CZProvinceprovinceWithDict:obj];

            [tempArrM addObject:province];

            

        }];

        

        _provinces = tempArrM;

        

    }


    return_provinces;

}


=================省市二级联动---------------=

#import "ViewController.h"

#import "CZProvince.h"


@interface ViewController () <UIPickerViewDelegate,UIPickerViewDataSource>



@property (weak,nonatomic)IBOutletUIPickerView *pickerView;

@property (weak,nonatomic)IBOutletUILabel *provinceLbl;

@property (weak,nonatomic)IBOutletUILabel *cityLbl;





// 省模型的数组

@property (nonatomic,strong)NSArray *provinces;


// 保存上一次显示或选中的省

@property (nonatomic,strong)CZProvince *selPro;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

 

    // 设置数据源.代理对象

    self.pickerView.dataSource =self;

    self.pickerView.delegate =self;

    

    // 设置默认的选中

    [selfpickerView:self.pickerViewdidSelectRow:0inComponent:0];

}



#pragma mark - 代理方法

//label上显示

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

    

    // 如果滚动的是第0,要刷新第1列城市数据

    if (component == 0) {

        [pickerView reloadComponent:1];

        // 选中第1列第0

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

    }


    // 获取省的名称获取市的名称

    // 得获取pickerView0列的选中行1列的选中行

    NSInteger selProIdx = [pickerViewselectedRowInComponent:0];

    NSInteger selCityIdx = [pickerViewselectedRowInComponent:1];

    

    // 设置省

    CZProvince *selPro =self.provinces[selProIdx];

    self.provinceLbl.text = selPro.name;

    

    // 设置市

//    self.cityLbl.text = selPro.cities[selCityIdx];

    self.cityLbl.text =self.selPro.cities[selCityIdx];//注意用保存的省得模型去赋值

    

}

//-自定义的显示,可以自己设置字体大小字体颜色

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

    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, WIDTH/3,30)];

    lbl.font=[UIFontsystemFontOfSize:15];

    if(component==0){

        LYHomeProvinceModal *provinceModal=self.provinceArr[row];

        lbl.text=provinceModal.state;

        

        

    }elseif(component==1){

        LYHomeProvinceModal *provinceModal=self.provinceModal;

        LYHomeCityModal *citymodal=provinceModal.cities[row];

        lbl.text=citymodal.city;

      

        

    }elseif(component==2){

        

        LYHomeCityModal *citymodal=self.citymodal;

         lbl.text=citymodal.areas[row];

        

    }

   

    return lbl;

}



// 返回显示的内容

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

    

    NSLog(@"titleForRow");

    

    // 如果是第0,直接返回省的名称

    if (component == 0) {

        CZProvince *pro =self.provinces[row];

        return pro.name;

    } else {

    

//        // 如果是第1,根据省确定市的数据

//        NSInteger selProIdx = [pickerView selectedRowInComponent:0];

//        CZProvince *selPro = self.provinces[selProIdx];//获取省得模型

//        

//        // 城市的数组

//        NSArray *cities = selPro.cities;

//        

//        // 返回内容

//        return cities[row];

        

        returnself.selPro.cities[row];

        

    }

}



#pragma mark - 数据源方法

// 返回组的数量

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {


    return 2;

}


// 返回每一组有多少行

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


    // 如果是第0,行数就是模型数量

    if (component == 0) {

        returnself.provinces.count;

    } else {


        // 如果是第1,根据第0列的省确定城市的数量

        // 1. 获取第0列选中下标

        NSInteger selProIdx = [pickerViewselectedRowInComponent:0];

        

        // 2. 根据下标去获取省模型

        CZProvince *selPro =self.provinces[selProIdx];

        

        

        // 保存选中的省模型

        self.selPro = selPro;

        

        returnself.selPro.cities.count;

    

        

//        // 3. 到模型中获取城市数量

//        return selPro.cities.count;

    

    }

}



#pragma mark - 懒加载

- (NSArray *)provinces {


    if (_provinces ==nil) {

        

        // 获取文件路径转为字典数组

        NSArray *dictArr = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"02cities.plist"ofType:nil]];

        

        // 遍历数组转为模型数组

//        for (<#initialization#>; <#condition#>; <#increment#>) {

//            <#statements#>

//        }


//        for (<#type *object#> in <#collection#>) {

//            <#statements#>

//        }

//        for (id obj in dictArr) {

//            NSDictionary *dict = (NSDictionary *)obj;

//            CZProvince *pro = [CZProvince provinceWithDict:dict];

//        }

        

        // 通过block遍历

        // 临时数组保存所有的模型

        NSMutableArray *tempArrM = [NSMutableArrayarrayWithCapacity:dictArr.count];

        [dictArr enumerateObjectsUsingBlock:^(NSDictionary *_Nonnull obj,NSUInteger idx, BOOL *_Nonnull stop) {

            

            CZProvince *province = [CZProvinceprovinceWithDict:obj];

            [tempArrM addObject:province];

            

        }];

        

        _provinces = tempArrM;

        

    }


    return_provinces;

}

@end

===============省市区三级联动============

<UIPickerViewDelegate,UIPickerViewDataSource>



//解析plist文件

-(void)parsePlistFile{

    NSString *path=[[NSBundlemainBundle]pathForResource:@"area.plist"ofType:nil];

    NSArray *arr=[NSArrayarrayWithContentsOfFile:path];

    self.arr=arr;

    

    NSLog(@"plist---arr---%@",arr);

    NSArray *modalArr=[LYHomeProvinceModalobjectArrayWithFilename:@"area.plist"];//字典转模型

   

    self.provinceArr=modalArr;

    

    


}



 UIPickerView *pickview=[[UIPickerViewalloc]initWithFrame:CGRectMake(0,HEIGHT/2,WIDTH,HEIGHT/2)];

    pickview.backgroundColor=[UIColorcolorWithWhite:0.9alpha:1];

    [self.viewaddSubview:pickview];

    pickview.delegate=self;

    pickview.dataSource=self;

    self.pickview=pickview;




#pragma mark - 代理方法

//选中一行 

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

   

    

    if(component==0){

         NSInteger provincenum = [pickerViewselectedRowInComponent:0];//1列选中的行--

     LYHomeProvinceModal *provincemodal  =self.provinceArr[provincenum];

        self.provinceModal=provincemodal;

        self.provinceStr=provincemodal.state;

        // 如果滚动的是第0,要刷新第1列城市数据

            [pickerView reloadComponent:1];

       

        // 选中第1列第0

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

        

        LYHomeCityModal *citymodal=provincemodal.cities[0];//每次刚启动时选择了省之后,市默认是0

        self.cityStr=citymodal.city;

        

        if(citymodal.areas.count>0){

        self.zoneStr=citymodal.areas[0];

        }else {

        self.zoneStr=@"";

        }

        

        if(self.provinceModal.cities[0]){//选择省的时候刷新完市,再刷新区

            // 如果滚动的是第1,要刷新第2列城市数据

            [pickerView reloadComponent:2];

            // 选中第2列第0

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

            

        }


        

    }elseif(component==1){

        LYHomeProvinceModal *provincemodal=self.provinceModal;

        NSInteger citynum = [pickerViewselectedRowInComponent:1];//2列选中的行--

        LYHomeCityModal *citymodal=provincemodal.cities[citynum];

        self.cityStr=citymodal.city;

        if(citymodal.areas.count>0){

        self.zoneStr=citymodal.areas[0];//选择了市之后区默认选择的是0行;

        }elseif(citymodal.areas.count==0){

        self.zoneStr=@"";

        }

       

         // 如果滚动的是第1,要刷新第2列城市数据

        [pickerView reloadComponent:2];

        // 选中第2列第0

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


    }elseif(component==2){

     NSInteger zonenum = [pickerViewselectedRowInComponent:2];//3列选中的行--

        LYHomeCityModal *citymodal=self.citymodal;

        if(citymodal.areas.count==0){

            self.zoneStr=@"";

        }else {

         self.zoneStr=citymodal.areas[zonenum];

        }


       

    }

    

    self.provinceAndCityAndZone(self.provinceStr,self.cityStr,self.zoneStr);//block调用传值

    

}



// 返回每一行显示的内容

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

    if(component==0){

     LYHomeProvinceModal *provinceModal=self.provinceArr[row];

    return provinceModal.state;

        

    }elseif(component==1){

        LYHomeProvinceModal *provinceModal=self.provinceModal;

        LYHomeCityModal *citymodal=provinceModal.cities[row];

        return citymodal.city;

        

    }elseif(component==2){

        

        LYHomeCityModal *citymodal=self.citymodal;

        return citymodal.areas[row];

    }

    return@"安徽";

}



#pragma mark - 数据源方法

// 返回有多少列""

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    

    return 3;

    

}


// 返回每一组有多少行

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

    if(component==0){//

        returnself.provinceArr.count;

    }elseif(component==1){//

        NSInteger provincenum = [pickerViewselectedRowInComponent:0];//1列选中的行

        

        // 2. 根据下标去获取省模型

       LYHomeProvinceModal *provinceModal =self.provinceArr[provincenum];

        self.provinceModal=provinceModal;//保存选中的省模型

        return provinceModal.cities.count;

    }elseif(component==2){//

        NSInteger citynum = [pickerViewselectedRowInComponent:1];//2列选中的行

        NSLog(@"-----%tu",citynum);

        LYHomeProvinceModal *provinceModal=self.provinceModal;

        LYHomeCityModal *citymodal=provinceModal.cities[citynum];//选中的市模型

        self.citymodal=citymodal;//保存市模型

        NSArray *zoneArr=citymodal.areas;

        NSLog(@"--zonearr--%tu",zoneArr.count);

        return zoneArr.count;

    }

    

    // 返回行数

    return 10;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值