#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{

NSArray *_nameArray;

}

@property (strong, nonatomic) UIPickerView *pickerView;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 162)];

self.pickerView.backgroundColor = [UIColor whiteColor];

self.pickerView.delegate = self;

self.pickerView.dataSource = self;

[self.view addSubview:self.pickerView];

[self.pickerView reloadAllComponents];//刷新UIPickerView

_nameArray = [NSArray arrayWithObjects:@"北京",@"上海",@"广州",@"深圳",@"重庆",@"武汉",@"天津",nil];

}

#pragma mark pickerview function

//返回有几列

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 3;

}

//返回指定列的行数

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

{

if (component==0) {

return  5;

} else if(component==1){

return  [_nameArray count];

}

return [_nameArray count];

}

//返回指定列,行的高度,就是自定义行的高度

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

return 20.0f;

}

//返回指定列的宽度

- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

if (component==0) {//iOS6边框占10+10

return  self.view.frame.size.width/3;

} else if(component==1){

return  self.view.frame.size.width/3;

}

return  self.view.frame.size.width/3;

}

// 自定义指定列的每行的视图,即指定列的每行的视图行为一致

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

if (!view){

view = [[UIView alloc]init];

}

UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/3, 20)];

text.textAlignment = NSTextAlignmentCenter;

text.text = [_nameArray objectAtIndex:row];

[view addSubview:text];

//隐藏上下直线

  [self.pickerView.subviews objectAtIndex:1].backgroundColor = [UIColor clearColor];

[self.pickerView.subviews objectAtIndex:2].backgroundColor = [UIColor clearColor];

return view;

}

//显示的标题

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

NSString *str = [_nameArray objectAtIndex:row];

return str;

}

//显示的标题字体、颜色等属性

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{

NSString *str = [_nameArray objectAtIndex:row];

NSMutableAttributedString *AttributedString = [[NSMutableAttributedString alloc]initWithString:str];

[AttributedString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, [AttributedString  length])];

return AttributedString;

}//NS_AVAILABLE_IOS(6_0);

//被选择的行

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

NSLog(@"HANG%@",[_nameArray objectAtIndex:row]);

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

效果如下:

iOSUIPickerView使用的更多相关文章

  1. ios-UIPickerView基本使用

    #import "ViewController.h" @interface ViewController ()<UIPickerViewDataSource,UIPicker ...

  2. iOS- UIPickerView餐厅点餐系统

    在餐厅里的点餐系统的核心控件就是UIPickerView 今天晚上在整理以前的项目笔记时,特意把UIPickerView单独拿出来,做了一个简陋的点餐道具. 因为没有素材图片,所有大家将就看看吧 0. ...

  3. webapp通用选择器:iosselect

    1,这个组件解决什么问题 在IOS系统中,safari浏览器的select标签默认展示样式和iOS-UIPickerView展示方式一致,形如下图: 这个选择器操作方便,样式优美.但是在安卓系统中展示 ...

随机推荐

  1. 【MyBatis】MyBatis之别名typeAliases标签的使用

    <configuration> <typeAliases> <typeAlias alias="Dept" type="cn.xdl.ent ...

  2. 【HTML】WWW简介

    www 什么是WWW www(world wide web),又称为万维网,或通常称为web,是一个基于超文本方式的信息检索服务工具. WWW的工作模式 C/S结构(client/server结构), ...

  3. 图形对象函数figure() 及 子图创建函数subplot()

    1 图像对象创建函数figure 创建图形Creates a new figure, 图形名既可以作为显示在图形窗口标题栏中的文本,也是该对象的名称 也可以通过mp.figure()获取(或激活)已创 ...

  4. mysql合并binlog

    例如: PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL DAY);

  5. nodejs的Express框架源码分析、工作流程分析

    nodejs的Express框架源码分析.工作流程分析 1.Express的编写流程 2.Express关键api的使用及其作用分析 app.use(middleware); connect pack ...

  6. 跟我学SharePoint 2013视频培训课程——使用垃圾箱(5)

    课程简介 第5天,在SharePoint 2013中 使用垃圾箱 视频 SharePoint 2013 交流群 41032413

  7. Eclipse安装PlantUML插件

    新技术的诞生和更新,新工具的发现和使用是两件让人开心的事情. 还记得Visio下苦苦的画流程图的时光吗,现在一切都变得so easy,因为有PlantUML! 官网:http://plantuml.c ...

  8. IE6-IE9兼容性问题列表及解决办法:锁表头的JQuery方案和非JQuery方案(不支持IE6,7,8)

    鉴于从IE8开始,IE不再支持css的expression了,所以以前依靠它完成锁表头的代码就全部失效了,面对新的浏览器,一切又要重新来过了. 现在所能找到的对于锁表头的方案主要有两种路子:一种是使用 ...

  9. IAR注释的快捷键

    1.注释的快捷键:Ctrl+K;取消注释:Ctrl+Shfit+K ... IAR注释代码的时候和MDK有很大的不同,以下简要说之:在注释代码的时候,你需如此操作: 注释一行,直接使用双斜杠”//“即 ...

  10. Android编译系统环境初始化过程分析

    Android源代码在编译之前,要先对编译环境进行初始化,其中最主要就是指定编译的类型和目标设备的型号.Android的编译类型主要有eng.userdebug和user三种,而支持的目标设备型号则是 ...