原文demo:

@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选择视图的更多相关文章

  1. iOS开发系列--视图切换

    概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...

  2. 浅谈iOS中的视图优化

    引言: 让我们来思考几个问题,你开发过的产品,它还有可以优化的地方吗?能增加它的帧率吗?能减少多余的CPU计算吗?是不是存在多余的GPU渲染?业务这点工作量对于越来越强大的设备面前显得微不足道,但作为 ...

  3. Swift - iOS中各种视图控制器(View Controller)的介绍

    在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来 ...

  4. Xamarin iOS教程之视图显示图像

    Xamarin iOS教程之视图显示图像 Xamarin iOS显示图像 在主视图中显示一个图像,可以让开发者的应用程序变的更有趣,例如,在一些应用程序开始运行时,都会通过图像来显示此应用程序的玩法或 ...

  5. IOS弹出视图 LewPopupViewController

    LewPopupViewController是一款IOS弹出视图软件.iOS 下的弹出视图.支持iPhone/iPad. 软件截图 使用方法 弹出视图 1 2 3 4 5 PopupView *vie ...

  6. IOS 如何选择delegate、notification、KVO?

    IOS 如何选择delegate.notification.KVO? 博客分类: IOS   前面分别讲了delegate.notification和KVO的实现原理,以及实际使用步骤,我们心中不禁有 ...

  7. iOS开发中视图控制器ViewControllers之间的数据传递

    iOS开发中视图控制器ViewControllers之间的数据传递 这里我们用一个demo来说明ios是如何在视图控制器之间传递重要的参数的.本文先从手写UI来讨论,在下一篇文章中讨论在storybo ...

  8. Xamarin.IOS之多视图

    欢迎大家加入以下开源社区 Xamarin-Cn:https://github.com/Xamarin-Cn Mvvmcross-Cn:https://github.com/Mvvmcross-Cn  ...

  9. (三十一)PickerView自定义视图

    例如选择国家,左边是名称右边是国家,不应该使用两列,而是自定义PickerView的一列,可以通过xib来实现. 注意,虽然PickerView也是一列,但是数据源方法是@required,所以必须实 ...

随机推荐

  1. c语言的枚举(遍历枚举)与数据类型总结

    一.枚举的概念 枚举是C语言中的一种基本数据类型,并不是构造类型,它可以用于声明一组常数.当一个变量有几个固定的可能取值时,可以将这个变量定义为枚举类型. 比如,你可以用一个枚举类型的变量来表示季节, ...

  2. [Machine Learning]学习笔记-线性回归

    模型 假定有i组输入输出数据.输入变量可以用\(x^i\)表示,输出变量可以用\(y^i\)表示,一对\(\{x^i,y^i\}\)名为训练样本(training example),它们的集合则名为训 ...

  3. python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言)

    python requests抓取NBA球员数据,pandas进行数据分析,echarts进行可视化 (前言) 感觉要总结总结了,希望这次能写个系列文章分享分享心得,和大神们交流交流,提升提升. 因为 ...

  4. hashlib,configparser,logging,模块

    一,hashlib模块 算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一 ...

  5. C++PRIMER 阅读笔记 第三章

    本章主要介绍 string vector 和 bitset, 不能贪多,现在本文主要介绍 string 与 vector 头文件中最好不要使用namespace std, 因为头文件会直接被预处理器放 ...

  6. 安装vue-cli时出现的错误,cmd 卡住

    今天在构建vue 时遇到个问题, cmd 执行 vue init webpack my-project 时 , 出现如下乱码, 然后 页面 卡住在 项目说明 ,操作不了, 最后发现是 nodejs 版 ...

  7. 记一下flex弹性布局

    flex弹性布局也越来越广泛的在我们代码中出现了,更加方便我们的布局.自己用了查,查了用,有些还是记不住,俗话说好脑子不如烂笔头,原来都是写在本子上的,很不幸的一次次的想翻的时候总是找不到,还是写博客 ...

  8. HDU 1216 Assistance Required 埃拉托色尼色筛法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1216 思路:色筛法 代码(1): #include<iostream>//-------- ...

  9. 第四届河南省ACM SUBSTRING 字符串处理

    SUBSTRING 时间限制: 1 Sec  内存限制: 128 MB 提交: 17  解决: 5 [提交][状态][讨论版] 题目描述 You are given a string input. Y ...

  10. C++雾中风景4:多态引出的困惑,对象的拷贝?

    C++作为一门面向对象的语言,自然具备了面向对象的三大特征:封装,继承,多态.在学习多态性质的过程中,发现了C++与其他语言很大的区别(坑?).在C++中的=操作符的使用与C++呈现的内存模型似乎并不 ...