#define k_pickerView_height 300
#define k_row_height 50
#define k_component_width (([UIScreen mainScreen].bounds.size.width)/3)
@interface ViewController (){
UIPickerView *testPickerView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self btnInit];
[self initPickerView];
}
#pragma mark - 初始化
-(void)initPickerView
{
testPickerView = [[UIPickerView alloc]init];
testPickerView.frame = CGRectMake(0, CGRectGetHeight(self.view.frame)-k_pickerView_height, CGRectGetWidth(self.view.frame), k_pickerView_height);
testPickerView.backgroundColor = [UIColor redColor];
[self.view addSubview:testPickerView];
//遵循代理协议
testPickerView.delegate = self;
testPickerView.dataSource = self;
//
testPickerView.showsSelectionIndicator = YES;
//获取分区数量
NSInteger num = testPickerView.numberOfComponents;
//获取某区--行尺寸
CGSize rowSize = [testPickerView rowSizeForComponent:1];
//获取某区---某行 的view
UIView *view = [testPickerView viewForRow:0 forComponent:1];
}
#pragma mark
#pragma mark - UIPickerView delegate
//几个分区
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
//每个分区几行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}
//行高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return k_row_height;
}
//对应分区---行---背景图片
//-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
//{
//
//
// return [UIView new];
//}
//每个分区宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return k_component_width;
}
//对应分区---行---普通文本内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//TODO:原理和tableView 一样,从左至右划分分区 (0-1-2) (0-0,0-1,0-2......) (1-0,1-1,1-2.....)......
// NSLog(@"component=%ld row=%ld",component,row);
return @"123";
}
//对应分区---行---多颜色文本内容
//-(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
// return nil;
//}
//滚动到当前区--行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"当前是:%ld区-%ld行",component,row);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)btnInit
{
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(120, 150, 100, 35)];
[btn1 setTitle:@"点击" forState:UIControlStateNormal];
[btn1 setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
btn1.tag = 1;
[btn1 setBackgroundColor:[UIColor lightGrayColor]];
[btn1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
-(void)click:(UIButton*)btn
{
//刷新所有
// [testPickerView reloadAllComponents];
//
//
// //刷新指定分区
[testPickerView reloadComponent:1];
//获取指定分区---当前行
NSInteger row = [testPickerView selectedRowInComponent:2];
//滚动到指定位置
[testPickerView selectRow:3 inComponent:2 animated:YES];
}