码迷,mamicode.com
首页 > 其他好文 > 详细

UI控件(UIPickerView)

时间:2016-03-05 20:30:33      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _item1 = [[NSArray alloc]initWithObjects:@"大类1",@"大类2", nil];
    _item2 = [NSDictionary dictionaryWithObjectsAndKeys:@[@"子类11",@"子类12"],@"大类1",@[@"子类21",@"子类22"],@"大类2",nil];
    _item3 = [NSDictionary dictionaryWithObjectsAndKeys:@[@"条目111",@"条目112"],@"子类11",@[@"条目121",@"条目122"], @"子类12",nil];
    
    _pickview = [[UIPickerView alloc]init];
    _pickview.dataSource = self;
    _pickview.delegate = self;
    
    _pickview.frame = CGRectMake(0, 10, 300, 100);
    _pickview.showsSelectionIndicator = YES;
    
    [self.view addSubview:_pickview];

}

// UIPickerViewDataSource协议必须实现的方法
// 确认该控件有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 3;
}

//UIPickerViewDataSource协议必须实现的方法
//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(0 == component){
        return _item1.count;
    }else if(1 == component){
        NSInteger item1 = [_pickview selectedRowInComponent:0];
        NSString  *item1Name = _item1[item1];
        NSArray *array = [_item2 valueForKey:item1Name];
      
        return array.count;
    }else if(2 == component){
        NSInteger item1 = [_pickview selectedRowInComponent:0];
        NSString  *item1Name = _item1[item1];
        NSArray *array = _item2[item1Name];
        NSInteger item2 = [_pickview selectedRowInComponent:1];
        NSString  *item2Name = array[item2];
        NSArray *array2 = _item3[item2Name];
        return array2.count;
    }else{
        return 0;
    }
}

//UIPickerViewDelegate协议要求实现
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(0 == component){
        return _item1[row];
    }else if(1 == component){
        NSInteger item1 = [_pickview selectedRowInComponent:0];
        NSString  *item1Name = _item1[item1];
        NSArray *array = _item2[item1Name];
        return array[row];
    }else if(2 == component){
        NSInteger item1 = [_pickview selectedRowInComponent:0];
        NSString  *item1Name = _item1[item1];
        NSArray *array = _item2[item1Name];
        NSInteger item2 = [_pickview selectedRowInComponent:1];
        NSString  *item2Name = array[item2];
        NSArray *array2 = _item3[item2Name];
        return array2[row];
    }else{
        return NULL;
    }
}

//获取选择的数据
//监听轮子的移动
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if(0 == component){    
        //重新加载第二列的数据
        NSLog(@"%@", _item1[row]);
        [_pickview reloadComponent:1];
    }else if(1 == component){
        //重新加载第三列的数据
        [_pickview reloadComponent:2];
        NSInteger item1 = [_pickview selectedRowInComponent:0];
        NSString  *item1Name = _item1[item1];
        NSArray *array = _item2[item1Name];
        NSLog(@"%@", array[row]);
    }else if(2 == component){
        NSInteger item1 = [_pickview selectedRowInComponent:0];
        NSString  *item1Name = _item1[item1];
        NSArray *array = _item2[item1Name];
        NSInteger item2 = [_pickview selectedRowInComponent:1];
        NSString  *item2Name = array[item2];
        NSArray *array2 = _item3[item2Name];
        NSLog(@"%@", array2[row]);
    }
}

//工具函数,遍历字典
-(void)describeDictionary:(NSDictionary *)dict
{
    NSArray *keys;
    int i, count;
    id key, value;
    
    keys = [dict allKeys];
    count = [keys count];
    for (i = 0; i < count; i++)
    {
        key = [keys objectAtIndex: i];
        value = [dict objectForKey: key];
        NSLog (@"Key: %@ for value: %@", key, value);
    }
}

@end

 

UI控件(UIPickerView)

标签:

原文地址:http://www.cnblogs.com/Fredric-2013/p/5245612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!