iOS PickerView选择视图
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{ UIPickerView * pickerView; NSMutableArray * listPicker;
NSMutableArray * listPicker2;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. [self CreatUI]; UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect]; back.frame = CGRectMake(, , , );
back.backgroundColor = [UIColor greenColor];
[back addTarget:self action:@selector(backk) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:back]; }
-(void)backk{ [self presentViewController:[[RootViewController alloc]init] animated:YES completion:nil]; } -(void)CreatUI{ pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(, , , )]; //显示选中框
pickerView.showsSelectionIndicator = YES;
pickerView.delegate =self;
pickerView.dataSource = self; [self.view addSubview:pickerView]; listPicker = [NSMutableArray array];
listPicker2 = [NSMutableArray array]; //dataSoure
for (int i =; i<; i++) { [listPicker addObject:[NSString stringWithFormat:@"%d",i]];
[listPicker2 addObject:[NSString stringWithFormat:@"%02d",i]]; } } /////相关代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return ;//列数
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return listPicker.count;//每列的row个数 } //列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component==) {
return ;
}else{
return ;
} }
//高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ if (component==) {
return ;
}else{ return ;
} }
//选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ } //返回当前行的内容 此处将是数组中数值添加到滚动的那个显示兰 批量操作显示的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component ==) {
return [NSString stringWithFormat:@"%@ 列",[listPicker objectAtIndex:row]];
}else{ return [NSString stringWithFormat:@"%@ 个",[listPicker objectAtIndex:row]];
} }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
一、两列 独立旋转
@interface LinkageViewController ()<UIPickerViewDelegate,UIPickerViewDataSource> { UIPickerView * pickerViewv; NSArray * listPicker;//sheng
NSArray * listPicker2;//shi }
@property(nonatomic,strong) UILabel * label; @end @implementation LinkageViewController - (void)viewDidLoad {
[super viewDidLoad];
[self CreatUI]; UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect]; back.frame = CGRectMake(, , , );
back.backgroundColor = [UIColor greenColor];
[back addTarget:self action:@selector(backk) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:back]; }
-(void)backk{ [self presentViewController:[[RootViewController alloc]init] animated:YES completion:nil]; } -(void)CreatUI{ pickerViewv = [[UIPickerView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, )]; //显示选中框
pickerViewv.showsSelectionIndicator = YES;
pickerViewv.delegate =self;
pickerViewv.dataSource = self; [self.view addSubview:pickerViewv]; listPicker = [NSMutableArray array];
listPicker2 = [NSMutableArray array]; //读取数据
NSString * path = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"plist"];
listPicker = [NSArray arrayWithContentsOfFile:path];
listPicker2 = [NSArray arrayWithArray:listPicker[][@"Cities"]]; //Label
//label的布局约束
self.label = [[UILabel alloc]initWithFrame:CGRectZero];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
self.label.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.label.textColor = [UIColor greenColor];
self.label.font = [UIFont systemFontOfSize:];
[self.label setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:self.label];
NSArray * labelTop = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[pickerViewv]-30-[_label(50)]" options: metrics:nil views:NSDictionaryOfVariableBindings(pickerViewv,_label)];
NSArray * labelH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[_label]-20-|" options: metrics:nil views:NSDictionaryOfVariableBindings(_label)];
[self.view addConstraints:labelTop];
[self.view addConstraints:labelH];
} /////相关代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return ;//列数
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ NSInteger rows =; switch (component) {
case :
rows = listPicker.count;
break;
case :
rows = listPicker2.count; default:
break;
} return rows;
} //列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component==) {
return ;
}else{
return ;
} }
//高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ if (component==) {
return ;
}else{ return ;
} } //选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component==) { listPicker2 = listPicker[row][@"Cities"];
[pickerViewv reloadComponent:];//刷新第几个
}else{ self.label.text = [NSString stringWithFormat:@"%@%@",listPicker[[pickerViewv selectedRowInComponent: ]][@"State"],listPicker2[[pickerViewv selectedRowInComponent:]][@"city"]]; } } //返回当前行的内容 此处将是数组中数值添加到滚动的那个显示兰 批量操作显示的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component ==) {
return listPicker[row][@"State"];
}else{ return listPicker2[row][@"city"];
} }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
二、第二列随着第一列刷新
@interface CustomViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{ UIPickerView * pickerView; NSMutableArray * listPicker;
NSMutableArray * listPicker2;
} @end @implementation CustomViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self CreatUI];
UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect]; back.frame = CGRectMake(, , , );
back.backgroundColor = [UIColor greenColor];
[back addTarget:self action:@selector(backk) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:back]; }
-(void)backk{ [self presentViewController:[[RootViewController alloc]init] animated:YES completion:nil]; } -(void)CreatUI{ pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height-)]; //显示选中框
pickerView.showsSelectionIndicator = YES;
pickerView.delegate =self;
pickerView.dataSource = self; [self.view addSubview:pickerView]; listPicker = [NSMutableArray array];
listPicker2 = [NSMutableArray array]; //dataSoure
for (int i =; i<; i++) { [listPicker addObject:[NSString stringWithFormat:@"%d",i]];
[listPicker2 addObject:[NSString stringWithFormat:@"%02d",i]]; } } /////相关代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return ;//列数
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return listPicker.count;//每列的row个数 } //列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component==) {
return ;
}else{
return ;
} }
//高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ if (component==) {
return ;
}else{ return ;
} }
//选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component==) {
//选中第一列。一般刷新第二列 // [pickerViewv reloadComponent:1];//刷新第几个 }else{
//选中第二列。一般输出 } }
#pragma mark 给pickerview设置字体大小和颜色等
-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ //可以自定义label
UILabel * pickerlabel = (UILabel*)view;
if (!pickerlabel) { pickerlabel = [[UILabel alloc] init];
pickerlabel.adjustsFontSizeToFitWidth = YES;
pickerlabel.textAlignment = NSTextAlignmentCenter;
[pickerlabel setBackgroundColor:[UIColor lightGrayColor]];
[pickerlabel setFont:[UIFont systemFontOfSize:]]; } pickerlabel.text = [self pickerView:pickerView titleForRow:row forComponent:component]; return pickerlabel; } //返回当前行的内容 此处将是数组中数值添加到滚动的那个显示兰 批量操作显示的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component ==) {
return [NSString stringWithFormat:@"%@ 列",[listPicker objectAtIndex:row]];
}else{ return [NSString stringWithFormat:@"%@ 个",[listPicker objectAtIndex:row]];
} }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
三、自定义显示View
iOS PickerView选择视图的更多相关文章
- iOS开发系列--视图切换
概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...
- 浅谈iOS中的视图优化
引言: 让我们来思考几个问题,你开发过的产品,它还有可以优化的地方吗?能增加它的帧率吗?能减少多余的CPU计算吗?是不是存在多余的GPU渲染?业务这点工作量对于越来越强大的设备面前显得微不足道,但作为 ...
- Swift - iOS中各种视图控制器(View Controller)的介绍
在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来 ...
- Xamarin iOS教程之视图显示图像
Xamarin iOS教程之视图显示图像 Xamarin iOS显示图像 在主视图中显示一个图像,可以让开发者的应用程序变的更有趣,例如,在一些应用程序开始运行时,都会通过图像来显示此应用程序的玩法或 ...
- IOS弹出视图 LewPopupViewController
LewPopupViewController是一款IOS弹出视图软件.iOS 下的弹出视图.支持iPhone/iPad. 软件截图 使用方法 弹出视图 1 2 3 4 5 PopupView *vie ...
- IOS 如何选择delegate、notification、KVO?
IOS 如何选择delegate.notification.KVO? 博客分类: IOS 前面分别讲了delegate.notification和KVO的实现原理,以及实际使用步骤,我们心中不禁有 ...
- iOS开发中视图控制器ViewControllers之间的数据传递
iOS开发中视图控制器ViewControllers之间的数据传递 这里我们用一个demo来说明ios是如何在视图控制器之间传递重要的参数的.本文先从手写UI来讨论,在下一篇文章中讨论在storybo ...
- Xamarin.IOS之多视图
欢迎大家加入以下开源社区 Xamarin-Cn:https://github.com/Xamarin-Cn Mvvmcross-Cn:https://github.com/Mvvmcross-Cn ...
- (三十一)PickerView自定义视图
例如选择国家,左边是名称右边是国家,不应该使用两列,而是自定义PickerView的一列,可以通过xib来实现. 注意,虽然PickerView也是一列,但是数据源方法是@required,所以必须实 ...
随机推荐
- JavaScript学习笔记(十)——高阶函数之map,reduce,filter,sort
在学习廖雪峰前辈的JavaScript教程中,遇到了一些需要注意的点,因此作为学习笔记列出来,提醒自己注意! 如果大家有需要,欢迎访问前辈的博客https://www.liaoxuefeng.com/ ...
- [转]the service mysql57 failed the most recent status[/br]mysql57 was not found解决办法
转自:http://forums.mysql.com/read.php?169,622722,622877#msg-622877 安装完mysql5.7.12后想要stop或者restart都会出现以 ...
- Python 面向对象(二) 特殊方法
一些Python特殊方法的汇总 __bases__ 类的基类,返回元祖__base__ 类的基类,也叫父类__call__ '类名()',类名加括号调用时执行的语句__class__ 对象或类 ...
- bootstrap_table_class表格样式实例
<div class="container"> <h2>表格</h2> <!--table标签级别的样式说明如下: ----.table ...
- C语言中静态申请内存遇到的错误分析
今天调试代码中,遇到了一个比较奇怪的打印,dump出来的数据只有前四位有值,其他后面的都为零. 出于直觉,应该是内存没有申请到.仔细核对代码之后,果真发现了一个语法错误,就是使用指针的指针时 ,对申请 ...
- JSP4(内置对象)
一.超级链接传值问题传值: <a href="href1.jsp?id=1&name=pkd&age=40">This is my JSP page. ...
- Scala入门系列(五):面向对象之类
定义类 // 定义类,包含field以及method class HelloWorld { private var name = "Leo" def sayHello() { pr ...
- 关于verilog中语句可不可综合
1)所有综合工具都支持的结构:always,assign,begin,end,case,wire,tri,aupply0,supply1,reg,integer,default,for,functio ...
- Nginx实现https网站配置
咱们下面以google代理实现的方式来实现https.注意这里的https与google的https无关. 代码如下.有备注/usr/local/nginx/config/vhost/hk.cn331 ...
- 【Codeforces 837D】Round Subset
http://codeforces.com/contest/837/problem/D 分解质因数,即第i个数的因子2的个数为c2[i],因子5的个数为c5[i],末尾零的个数就是min{Σc2[i] ...