#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. mysql5.7忘记密码时,修改root密码

    (1).由于MySQL5.7在安装完后,第一次启动时,会在root目录下生产一个随机的密码,文件名为 .mysql_secret 所以,登录时需要用随机密码登录,然后通过以下命令修改密码 “SET P ...

  2. 简单几步让CentOS系统时间同步(转)

    在使用CentOS系统的时候,我们可能会遇到时间不准的问题,那我们如何解决这个我问题呢,下面就来教大家一个CentOS系统时间同步的方法,希望大家可以解决自己所存在的疑问. CentOS系统时间同步的 ...

  3. 【Java】详解java对象的序列化

    目录结构: contents structure [+] 序列化的含义和意义 使用对象流实现序列化 对象引用的序列化 自定义序列化 采用实现Serializable接口实现序列化 采用实现Extern ...

  4. Git管理工具对比(GitBash、EGit、SourceTree)(转载)

    Git管理工具对比(GitBash.EGit.SourceTree) GitBash是采用命令行的方式对版本进行管理,功能最为灵活强大,但是由于需要手动输入希望修改的文件名,所以相对繁琐. EGit是 ...

  5. Error Code: 1030. Got error -1 from storage engine

    这个问题通常是数据库可以建表,旧表可以插入数据,正常:可是新表无法插入数据,无法改名等操作: 先从文件权限找方法,没法解决: 在网上搜了一通,大家都说的磁盘满了,但是我们的磁盘还空着呢! 后来,发现! ...

  6. 关于PKCS的文档资料

    关于PKCS的文档资料,在这里查找: http://www.emc.com/emc-plus/rsa-labs/standards-initiatives/public-key-cryptograph ...

  7. 类名.class和getClass()区别

    class叫做“类字面量”,因class是关键字, 所以class编译时确定,getclass()运行时根据实际实例确定.String.class 是能对类名的引用取得在内存中该类型class对象的引 ...

  8. Android系统版本与API级别对照表

    对照表 API Level 最初Android版本 Linux内核版本 首次发布日期 后续Android版本 28 9 Unknown 2018-07-02(Beta 3) - 27 8.1 4.10 ...

  9. ELK 的插件安装(head)

    这里我装了一个head插件和kopf的插件 ./plugin install mobz/elasticsearch-head ./plugin install lmenezes/elasticsear ...

  10. JDK1.5新特性,基础类库篇,System类

    一. 背景 System.getenv(String)方法继续有效:增加了新的System.getenv()方法,返回保存环境变量的Map<String,String>. 同时增加了以纳秒 ...