关闭

UI - UIPickerView

标签: UIPickerViewUIPickerView总结
204人阅读 评论(0) 收藏 举报
分类:
#import "ViewController.h"

#define kResourceArr_1 @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"]
#define kResourceArr_2 @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j"]

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    [self configurePickerView];
}
- (void)configurePickerView
{
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(25, 100, 300, 200)];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    // 显示选中指示器, 默认为 NO
    pickerView.showsSelectionIndicator = YES;
    // 获取列数,只读
    pickerView.numberOfComponents;
    
    //刷新整个 pickerView
    [pickerView reloadAllComponents];
    //刷新某列
    [pickerView reloadComponent:0];
    //选中某列
    [pickerView selectedRowInComponent:0];
    //选中某行某列
    [pickerView selectRow:9 inComponent:1 animated:YES];
    
    [self.view addSubview:pickerView];
}

#pragma mark - dataSource

//列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//每列的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 10;
}

#pragma mark - delegate

//通过添加NSString展示内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return kResourceArr_1[row];
    }else{
        return kResourceArr_2[row];
    }
}
//通过添加NSAttributedString展示内容
//-(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
//    return nil;
//}
//行高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 30;
}
//列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 100;
}
//通过添加 View 展示内容
//-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
//{
//    UIView *rowView_1 = [[UIView alloc]init];
//    rowView_1.backgroundColor = [UIColor cyanColor];
//    UIView *rowView_2 = [[UIView alloc]init];
//    rowView_2.backgroundColor = [UIColor magentaColor];
//    
//    if (component == 0) {
//        return rowView_1;
//    }else{
//        return rowView_2;
//    }
//}
//成功选择后触发的代理
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"选择的是%ld列%ld行",component,row);
}

@end

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:206262次
    • 积分:4225
    • 等级:
    • 排名:第7210名
    • 原创:238篇
    • 转载:37篇
    • 译文:0篇
    • 评论:24条
    最新评论