首页 > 代码库 > iOSUIPickerView使用

iOSUIPickerView使用

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #ba2da2 } span.s1 { color: #78492a } span.s2 { } span.s3 { color: #ba2da2 } span.s4 { color: #000000 } span.s5 { color: #703daa }</style>

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{

    

    NSArray *_nameArray;

}

 

@property (strong, nonatomic) UIPickerView *pickerView;

 

@end

 

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #ba2da2 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400 } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } p.p9 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a } p.p10 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #008400 } p.p11 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400; min-height: 21.0px } span.s1 { color: #78492a } span.s2 { } span.s3 { color: #ba2da2 } span.s4 { color: #000000 } span.s5 { color: #703daa } span.s6 { color: #4f8187 } span.s7 { color: #3e1e81 } span.s8 { color: #272ad8 } span.s9 { color: #008400 } span.s10 { font: 18.0px "PingFang SC"; color: #008400 } span.s11 { font: 18.0px "PingFang SC" } span.s12 { font: 18.0px Menlo } span.s13 { color: #d12f1b }</style>

#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];

    //隐藏上下直线

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } span.s1 { color: #000000 } span.s2 { color: #ba2da2 } span.s3 { color: #4f8187 } span.s4 { } span.s5 { color: #3e1e81 } span.s6 { color: #272ad8 }</style>

  [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使用