黑马程序员——iOS 开发学习---UIPickerView控件的应用
2016-01-20 11:40
513 查看
——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-
.H
.M
.H
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate> { NSMutableArray* _provinceArray; NSMutableArray* _cityArray; } @end
.M
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //处理数据源 _provinceArray=[[NSMutableArray alloc]initWithObjects:@"河南省",@"安徽省",@"四川省", nil]; NSMutableArray* array1=[[NSMutableArray alloc]initWithObjects:@"郑州", @"新郑",@"周口",@"信阳",@"新乡",nil]; NSMutableArray* array2=[[NSMutableArray alloc]initWithObjects:@"合肥",@"芜湖",@"六安",nil]; NSMutableArray* array3=[[NSMutableArray alloc]initWithObjects:@"汶川",@"南充",@"绵阳",nil]; //把三个小数组装进一个大数组(模范二维数组) _cityArray=[[NSMutableArray alloc ]initWithObjects:array1,array2,array3, nil]; //处理UI 下面两者继承的父类不同,没有直接关系。 //UIDatePicker : UIControl //UIPickerView : UIView 它的高度是固定的:162 180 216 UIPickerView *pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(30, 150, 260, 180)]; pickerView.backgroundColor=[UIColor grayColor]; //设置数据源代理 //@optional:表明这些方法是可选的 //@requied: 表明这些方法是必选的(必须实现的,不实现就找不到方法,就会崩溃) //设置代理 pickerView.dataSource=self; pickerView.delegate=self; pickerView.showsSelectionIndicator=YES; //默认选择那一列的哪一行 [pickerView selectRow:0 inComponent:0 animated:YES];//(第0列的第0行) [self.view addSubview:pickerView]; //输出一个空间的的坐标大小 //NSLog(@"%@",NSStringFromCGRect(pickerView.frame)); // Do any additional setup after loading the view, typically from a nib. } #pragma mark UIPickerViewDataSource //设置PickerView列数 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } //设置PickerView行数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ //如果是第0列返回三行 //如果不是第0列,那么需要根据第0列选择第几行,分别来设置第1列行数 if (component==0) { return _provinceArray.count; }else{ //获取第0列你选择的哪一行 int number=[pickerView selectedRowInComponent:0]; //根据number(第0列选择了第number) NSMutableArray *array=[_cityArray objectAtIndex:number]; return array.count; } } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component==0) { NSString* title=[_provinceArray objectAtIndex:row]; return title; }else{ int number=[pickerView selectedRowInComponent:0]; //取出对应的小数组 NSMutableArray *array=[_cityArray objectAtIndex:number]; //返回小数组元素 NSString* title=[array objectAtIndex:row]; return title; } } #pragma mark 刷新数据源 UIPickerViewDelegate代理 //只要行发生了变化都会调用该方法 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component==0) { //[pickerView reloadAllComponents];//刷新所有的列 效率比较低,因为刷新的是所有的列 //指定列刷新, [pickerView reloadComponent:1];//第一列的行数改变的时候要刷新 } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- 用Ueditor存入数据库带HTML标签的文本,从数据库取出来后,anjular用ng-bind-html处理带HTML标签的文本
- 构建轻量级的Table View注意事项[UIKit]
- 你所不知道的权限-Custom permission issue
- 解决虚拟机中使用ntpdate报错:ntpdate[46700]: no server suitable for synchronization found
- 获得Version和Build版本号
- 从request中获取项目的一些路径
- Mosquitto 在 Mac上安装调试
- iOS UItableViewCell的复用机制,原理解答
- UITableViewCell的点击事件而不是select事件
- Animation动画详解(五)——ValueAnimator高级进阶(一)
- 使用Mosquitto-Auth-Plugin对mqtt客户端进行验证
- 关于ios7UI的适配【转】---Test
- UItableViewCell里面的UIButton点击不响应的诡异现象
- Scala - error: not found: value SortedMap
- UESTC - 1264:人民币的构造【规律】
- BezierPath
- UIPanGestureRecognizer使用小结
- 附录_UI开发推荐颜色
- Android UI 优化——使用HierarchyViewer工具
- ContactsUI(简介)