#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. jquery.inputmask 输入框input输入内容格式限制插件

    jQuery Input Mask plugin http://robinherbots.github.io/jquery.inputmask README.md jquery.inputmask C ...

  2. OCR OneNote

    文章地址:https://www.cnblogs.com/Charltsing/p/OneNoteOCR.html 前段时间有人问我能不能通过OneNote扫描图片,并返回文本.经过几天的测试,以及对 ...

  3. Git 配置(分布式版本控制系统)

    1.Mac Git 配置文件 既然已经在系统上安装了 Git,你会想要做几件事来定制你的 Git 环境.每台计算机上只需要配置一次,程序升级时会保留配置信息.你可以在任何时候再次通过运行命令来修改它们 ...

  4. 关于ps cs5的一些问题

    一.photoshop cs5 默认在窗口中浮动方法 1.打开“编辑>首选项>界面”在“面板和文档”里把“以选项卡方式打开图像”的勾选去掉 2.点击菜单栏“窗口>排列>使所有内 ...

  5. C++的字符串格式化库

    这里向大家介绍一个C++的字符串格式化库,叫cpptempl,这个库支持对字符串格式的条件,循环,变量插入.看上去很不错,只不过其是基于boost库的. 下面是一个例子: 1 2 3 4 5 6 7 ...

  6. 转:ECharts图表组件入门教程之Theme:ECharts图表的皮肤是什么?如何给图表换主题(皮肤)Theme?

    一.什么是ECharts图表的皮肤(主题)? 针对这个问题我只能这样回答,ECharts图表的主题(皮肤)就犹如人的衣服一样,是用来衬托和渲染主体,使其变得更加美观好看的目的.你去过ECharts图表 ...

  7. CSS加DIV布局

    第一种: <div> <div class="right"> <p></p> <p></p> <p&g ...

  8. Android 启动、绘制、显示过程

    Activity 启动过程: startActivity()-> Instrumentation.execStartActivity()-> Binder->ActivityMana ...

  9. PHP将CMYK颜色值和RGB颜色相互转换的例子

    PHP将CMYK颜色值和RGB颜色相互转换的例子 function hex2rgb($hex) { $color = str_replace('#','',$hex); $rgb = array('r ...

  10. 运行jar乱码问题

    请使用 1.java -jar  -Dfile.encoding=utf-8 dapao.jar 2.请使用URLDecode,URLEncode 3.请使用unicode编码格式 bat运行当前目录 ...