//
// ViewController.m
// pickview测试
//
// Created by ibokan on 16/7/14.
// Copyright © 2016年 ivna. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()< UIPickerViewDelegate , UIPickerViewDataSource >
@property ( strong , nonatomic ) UIPickerView *pickV;
@property ( strong , nonatomic ) NSArray *infoArr;
@end
@implementation ViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
[ self . view addSubview : self . pickV ];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark-----datasource
- ( NSInteger )numberOfComponentsInPickerView:( UIPickerView *)pickerView
{
return 1 ;
}
-( void )pickerView:( UIPickerView *)pickerView didSelectRow:( NSInteger )row inComponent:( NSInteger )component
{
NSLog ( @"%@" , self . infoArr [row]);
}
/**
*
*
* @param pickerView
* @param row 行
* @param component 第几列
*
* @return 返回当前行的内容
*/
-( NSString *)pickerView:( UIPickerView *)pickerView titleForRow:( NSInteger )row forComponent:( NSInteger )component
{
return self . infoArr [row];
}
/**
*
*
* @param pickerView
* @param component列
*
* @return 行的宽度
*/
-( CGFloat )pickerView:( UIPickerView *)pickerView widthForComponent:( NSInteger )component
{
return 100 ;
}
// returns the # of rows in each component..
- ( NSInteger )pickerView:( UIPickerView *)pickerView numberOfRowsInComponent:( NSInteger )component
{
// if (component==0) {
// return
// }
return self . infoArr . count ;
}
/**
*
*
* @param pickerView
* @param row
* @param component
* @param view
*
* @return pickv的显示文本
*/
- ( UIView *)pickerView:( UIPickerView *)pickerView viewForRow:( NSInteger )row forComponent:( NSInteger )component reusingView:( UIView *)view{
UILabel * pickerLabel = ( UILabel *)view; //强制转换成lab
if (!pickerLabel){
pickerLabel = [[ UILabel alloc ] init ];
pickerLabel. font = [ UIFont systemFontOfSize : 8.0 ];
pickerLabel. adjustsFontSizeToFitWidth = YES ; //让他适应文本的宽度
[pickerLabel setTextAlignment : NSTextAlignmentCenter ]; //居中
[pickerLabel setBackgroundColor :[ UIColor clearColor ]];
[pickerLabel setFont :[ UIFont boldSystemFontOfSize : 15 ]];
/**
1 + systemFontOfSize
2 + boldSystemFontOfSize
3 + italicSystemFontOfSize
1 中是系统的默认方法
2 是字体 bold :醒目的;雄浑的;突出的,使用后会加粗字体
3 字体为斜体 italic [i'tælik] n. 斜体,斜体字
*/
}
pickerLabel. text =[ self pickerView :pickerView titleForRow :row forComponent :component];
return pickerLabel;
}
#pragma mark----- get
-( UIPickerView *)pickV
{
if (! _pickV ) {
_pickV =[[ UIPickerView alloc ] initWithFrame : CGRectMake ( 50 , 100 , 200 , 200 )];
_pickV . dataSource = self ;
_pickV . delegate = self ;
_pickV . showsSelectionIndicator = YES ;
}
return _pickV ;
}
-( NSArray *)infoArr
{
if (! _infoArr ) {
_infoArr = @[ @"默认" , @"应届生" , @"1年以内" , @"1-3年" , @"3-5年" , @"5-10年" , @"10年以上" ] ;
}
return _infoArr ;
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
// ViewController.m
// pickview测试
//
// Created by ibokan on 16/7/14.
// Copyright © 2016年 ivna. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()< UIPickerViewDelegate , UIPickerViewDataSource >
@property ( strong , nonatomic ) UIPickerView *pickV;
@property ( strong , nonatomic ) NSArray *infoArr;
@end
@implementation ViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
[ self . view addSubview : self . pickV ];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark-----datasource
- ( NSInteger )numberOfComponentsInPickerView:( UIPickerView *)pickerView
{
return 1 ;
}
-( void )pickerView:( UIPickerView *)pickerView didSelectRow:( NSInteger )row inComponent:( NSInteger )component
{
NSLog ( @"%@" , self . infoArr [row]);
}
/**
*
*
* @param pickerView
* @param row 行
* @param component 第几列
*
* @return 返回当前行的内容
*/
-( NSString *)pickerView:( UIPickerView *)pickerView titleForRow:( NSInteger )row forComponent:( NSInteger )component
{
return self . infoArr [row];
}
/**
*
*
* @param pickerView
* @param component列
*
* @return 行的宽度
*/
-( CGFloat )pickerView:( UIPickerView *)pickerView widthForComponent:( NSInteger )component
{
return 100 ;
}
// returns the # of rows in each component..
- ( NSInteger )pickerView:( UIPickerView *)pickerView numberOfRowsInComponent:( NSInteger )component
{
// if (component==0) {
// return
// }
return self . infoArr . count ;
}
/**
*
*
* @param pickerView
* @param row
* @param component
* @param view
*
* @return pickv的显示文本
*/
- ( UIView *)pickerView:( UIPickerView *)pickerView viewForRow:( NSInteger )row forComponent:( NSInteger )component reusingView:( UIView *)view{
UILabel * pickerLabel = ( UILabel *)view; //强制转换成lab
if (!pickerLabel){
pickerLabel = [[ UILabel alloc ] init ];
pickerLabel. font = [ UIFont systemFontOfSize : 8.0 ];
pickerLabel. adjustsFontSizeToFitWidth = YES ; //让他适应文本的宽度
[pickerLabel setTextAlignment : NSTextAlignmentCenter ]; //居中
[pickerLabel setBackgroundColor :[ UIColor clearColor ]];
[pickerLabel setFont :[ UIFont boldSystemFontOfSize : 15 ]];
/**
1 + systemFontOfSize
2 + boldSystemFontOfSize
3 + italicSystemFontOfSize
1 中是系统的默认方法
2 是字体 bold :醒目的;雄浑的;突出的,使用后会加粗字体
3 字体为斜体 italic [i'tælik] n. 斜体,斜体字
*/
}
pickerLabel. text =[ self pickerView :pickerView titleForRow :row forComponent :component];
return pickerLabel;
}
#pragma mark----- get
-( UIPickerView *)pickV
{
if (! _pickV ) {
_pickV =[[ UIPickerView alloc ] initWithFrame : CGRectMake ( 50 , 100 , 200 , 200 )];
_pickV . dataSource = self ;
_pickV . delegate = self ;
_pickV . showsSelectionIndicator = YES ;
}
return _pickV ;
}
-( NSArray *)infoArr
{
if (! _infoArr ) {
_infoArr = @[ @"默认" , @"应届生" , @"1年以内" , @"1-3年" , @"3-5年" , @"5-10年" , @"10年以上" ] ;
}
return _infoArr ;
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
@end