美文网首页iOS开发
UIPickerView的使用

UIPickerView的使用

作者: 追逐_chase | 来源:发表于2019-04-16 08:56 被阅读0次
    UIPickerView继承自UIView,它的用法类似UITableView,需要设置数据源代理UIPickerViewDataSource和交互代理UIPickerViewDelegate
    基本使用
    • 创建并设置代理,我是用SB创建
      self.pickView.dataSource = self;
        self.pickView.delegate = self;
    
    • 遵循 UIPickerViewDataSource协议方法
    //返回d多少列
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
       
        return 1;
    }
    
    //每一列返回多好行
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        
        return 10;
    }
    
    • 遵循 UIPickerViewDelegate协议方法
    //每行显示的内容
    - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        
        return @"100";
    }
    //每行可以显示UIView视图 下面的这个方法
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    }
    
    //选中那一列的哪一行
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        
    }
    
    
    使用二级联动
    二级联动.png
    数据模型
    image.png image.png image.png
    初始化控件
    #import "CCStateTF.h"
    
    #import "CCStateItem.h"
    
    @interface CCStateTF()<UIPickerViewDataSource,UIPickerViewDelegate>
    
    @property (nonatomic, weak) UIPickerView *pickView;
    
    @property (nonatomic, strong) NSArray *datas;
    
    ///第一列选中的row数 既是省份
    @property (nonatomic, assign) NSInteger selectIndexRow;
    
    @end
    
    @implementation CCStateTF
    
    - (instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            [self setupChildContent];
        }
        
        return self;
    }
    
    
    - (void)awakeFromNib {
        [super awakeFromNib];
        [self setupChildContent];
    }
    
    
    - (void)setupChildContent{
        [self pickView];
    }
    
    - (void)initText {
        [self pickerView:self.pickView didSelectRow:0 inComponent:0 ];
    }
    
    //懒加载
    - (UIPickerView *)pickView {
        if (!_pickView) {
            UIPickerView *pickView = [[UIPickerView alloc] init];
            pickView.dataSource = self;
            pickView.delegate = self;
            self.inputView = pickView;
            self.pickView = pickView;
            
            
        }
        
        return _pickView;
    }
    
    //初始化数据
    - (NSArray *)datas{
        if (!_datas) {
            
            NSString *filePath = [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil];
            NSArray *data = [NSArray arrayWithContentsOfFile:filePath];
            NSMutableArray *tempData = [NSMutableArray array];
            
            for (NSDictionary *dic in data) {
                CCStateItem *item = [CCStateItem stateItemWithDictionary:dic];
                [tempData addObject:item];
            }
            
            _datas = tempData;
            
        }
        
        return _datas;
    }
    

    代理方法

    //返回2列
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        
        return 2;
    }
    
    //每列返回多少行
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        
        if (component == 0) {
            //第一列返回的个数
            return self.datas.count;
        }
        CCStateItem *item = self.datas[self.selectIndexRow];
        return item.cities.count;
    }
    
    //返回的内容
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        
        //第一列
        if (component == 0) {
            CCStateItem *item = self.datas[row];
            return item.name;
        }
        //第二列
        CCStateItem *compItem = self.datas[self.selectIndexRow];
        return compItem.cities[row];
    }
    
    //选中那l一列的那一行
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        if (component == 0) {
            //获取第一列 选中的是哪一行
            self.selectIndexRow = row;
            //选中
            [pickerView selectRow:0 inComponent:1 animated:YES];
            //刷新数据
            [pickerView reloadAllComponents];
            
        }
        
        //获取到选中的
        CCStateItem *item =  self.datas[self.selectIndexRow];
        NSString *name = item.name;
        //获取第一列x选中的 行数
        NSInteger seletCity = [pickerView selectedRowInComponent:1];
        NSString *city = item.cities[seletCity];
        
        self.text = [NSString stringWithFormat:@"%@-%@",name,city];
        
       
        
    }
    
    

    相关文章

      网友评论

        本文标题:UIPickerView的使用

        本文链接:https://www.haomeiwen.com/subject/gjlrwqtx.html