标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 [self layoutUI]; 13 } 14 15 - (void)didReceiveMemoryWarning { 16 [super didReceiveMemoryWarning]; 17 // Dispose of any resources that can be recreated. 18 } 19 20 - (void)layoutUI { 21 UIPickerView *pikVCustom = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; 22 pikVCustom.center = self.view.center; 23 pikVCustom.dataSource = self; 24 pikVCustom.delegate = self; 25 [self.view addSubview:pikVCustom]; 26 } 27 28 #pragma mark - PickerView 29 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 30 return 2; //返回组件列数 31 } 32 33 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 34 return 10; //返回组件行数 35 } 36 37 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 38 NSString *strTitle = @""; 39 if (component == 0) { 40 strTitle = [NSString stringWithFormat:@"%2ld", row+1]; 41 } else { 42 strTitle = [NSString stringWithFormat:@"比较长的字符串,其中%ld", row+1]; 43 } 44 return strTitle; //返回组件行标题 45 } 46 47 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 48 return component == 0 ? 40 : 280; //返回组件宽度 49 } 50 51 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { 52 return 40; //返回组件行高度 53 } 54 55 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4579331.html