#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. 【C#】反编译C#应用程序

    下面的使用的使用是笔者使用的一个简单的程序,就是弹出一个消息框. 这里通过 ildasm 反编译 应用程序得到 il 文件,然后对 il 文件进行修改,修改内容后,再通过ilasm编译为应用程序.il ...

  2. 图片标注工具LabelImg使用教程

    1.进入labelImg-master文件夹,在空白处使用 “Shift+鼠标右键” ,选择在此处打开命令窗口,依次输入下面语句即可打开软件. pyrcc4 -o resources.py resou ...

  3. 【sql】CHARINDEX

    语法:CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] ) 参数: 1)expressionToFind 包含 ...

  4. Easyui中 messager出现的位置

    $.messager.alert 弹出框的位置随页面的长度越大越靠下. $.messager.alert('消息','只能对单个客户进行清款!','info'); 弹出的位置 太靠下方.修改为: $. ...

  5. Android HttpURLConnection源代码分析

    Android HttpURLConnection源代码分析 之前写过HttpURLConnection与HttpClient的差别及选择.后来又分析了Volley的源代码. 近期又遇到了问题,想在V ...

  6. 基于C/S模式的android手机与PC机通信系统的开发

    原文链接: http://blog.csdn.net/nupt123456789/article/details/8213486 基于C/S模式的android手机与PC机通信系统的开发 作者:郑海波 ...

  7. STVD中将现有工程重命名为另一个工程

    http://blog.csdn.net/sy_lixiang/article/details/47273649 例子:把工程名为Template的工程改为color,把左边红圈部分重命名为右面的名字 ...

  8. [转]SSH 原理和基本使用:ssh 安全配置 以及ssh key 认证登录

    一.什么是 SSH ? SSH全称(Secure SHell)是一种网络协议,顾名思义就是非常安全的shell,主要用于计算机间加密传输.早期,互联网通信都是基于明文通信,一旦被截获,内容就暴露无遗. ...

  9. Java – How to convert String to Char Array

    Java – How to convert String to Char ArrayIn Java, you can use String.toCharArray() to convert a Str ...

  10. U811.1接口EAI系列之六--物料上传--VB语言

    1. 业务系统同步U811.1存货档案通用方法. 2.具体代码处理如下: 作者:王春天 2013-11-06 地址:http://www.cnblogs.com/spring_wang/p/34098 ...