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

UIPickView的使用和

2016-04-28 12:29 387 查看
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) IBOutlet
UIPickerView *pickerView;

@property (weak,
nonatomic) IBOutlet
UILabel *provinceLbl;

@property (weak,
nonatomic) IBOutlet
UILabel *cityLbl;

// 省模型的数组

@property (nonatomic,
strong) NSArray *provinces;

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

@property (nonatomic,
strong)
LBProvince *selPro;

@end

@implementation ViewController

- (void)viewDidLoad {

[super
viewDidLoad];

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

self.pickerView.dataSource =
self;

self.pickerView.delegate =
self;

//
设置默认的选中

[self
pickerView:self.pickerView
didSelectRow:0
inComponent: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:0
inComponent:1
animated:YES];

}

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

//
得获取pickerView第0列的选中行
第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 = [NSArray
arrayWithContentsOfFile:[[NSBundle
mainBundle] 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 = [NSMutableArray
arrayWithCapacity:dictArr.count];

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

CZProvince *province = [CZProvince
provinceWithDict:obj];

[tempArrM addObject:province];

}];

_provinces = tempArrM;

}

return
_provinces;

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