#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

效果如下:

最新文章

  1. 很不错的Intent用法 适用于正在开发的伙伴。自己看到了,也分享给大家吧。
  2. CentOS7 Java安装
  3. 最简MacOs10.8安装
  4. 王家卫i
  5. cf div2 238 D
  6. linux入门教程(八) Linux磁盘管理
  7. LinearLayout使用tips
  8. 【hibernate】spring+ jpa + hibername 配置过程遇到的问题
  9. Longest Consecutive Sequence 解答
  10. 用JAVA自己画一张二维码
  11. Spring中@Transactional事务回滚(含实例详细讲解,附源码)
  12. 使用清华开源镜像安装tensorflow
  13. RecyclerView实现一个页面有多种item,每个item有多个view,并且可以让任意item的任意view自定义监听,通过接口方法进行触发操作
  14. Python内置函数(57)——setattr
  15. Bigger-Mai 养成计划,Python基础巩固二
  16. windows 下 mysql服务的注册和删除
  17. CSS3 利用border-radius实现椭圆角
  18. 通过xml处理sql语句时对小于号与大于号的处理转换
  19. float 、double 加减乘除出现小数位多出的问题
  20. Java反射获取对象VO的属性值(通过Getter方法)

热门文章

  1. iOS11新特性之LargeTitle
  2. linux记录sftp命令
  3. [译]Spring Boot 构建一个RESTful Web服务
  4. Android中五种常用的menu
  5. [转]IC行业的牛人
  6. iOS 再谈 代理传值,block反向传值
  7. 微信小程序调用蓝牙功能控制车位锁
  8. iOS缓存-内存缓存
  9. 【Unity】7.4 游戏外设输入
  10. Python Socket网络编程详解