• UI控件(UIPickerView)


    @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
  • 相关阅读:
    《Metasploit 渗透测试魔鬼训练营》 攻击机无法攻击靶机
    Ubuntu 解压 RAR
    verilog实验2:基于FPGA的59秒计时器设计
    verilog实验1:基于FPGA蜂鸣器演奏乐曲并数码管显示
    Java基础之反射和动态代理
    Redis初探
    Rest(表述性状态转移)
    深入理解MVC模式
    @Controller和@RestController的区别
    solrconfig.xml和schema.xml说明
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/5245612.html
Copyright © 2020-2023  润新知