美文网首页
UIPickerView 使用详解

UIPickerView 使用详解

作者: 树根曰 | 来源:发表于2016-11-03 17:26 被阅读0次

    效果:

    72F2FA61-5CE4-4D2F-BA1C-B30146F2956E.png

    废话不多少,直接上代码:

    #import "ViewController.h"
    
    @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
    
    {
        NSArray *_provinceArr;
        NSArray *allArr;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self loadData];
        [self loadPickerView];
    }
    

    /** 设置数据源 并初始化控件*/

    /** 数据源 */
    - (void)loadData {
        //省这个数组的位置位于第0列
        _provinceArr = [[NSArray alloc] initWithObjects:@"河北省",@"河南省",@"山东省", nil];
        
        //小数组对应的是省数组中的省份
        NSArray *heiBeiArr = [[NSArray alloc] initWithObjects:@"石家庄",@"唐山",@"秦皇岛",@"邯郸",@"保定",@"邢台", nil];
        NSArray *heNanArr = [[NSArray alloc] initWithObjects:@"郑州",@"新乡",@"安阳",@"驻马店", nil];
        NSArray *shangDongArr = [[NSArray alloc] initWithObjects:@"日照",@"济南",@"淄博",@"青岛",@"德州", nil];
        allArr = @[heiBeiArr,heNanArr,shangDongArr];
    }
    /** 初始化控件 */
    - (void)loadPickerView {
        
        UIPickerView *pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 400)];
        pickView.delegate = self;
        pickView.dataSource = self;
        pickView.showsSelectionIndicator = YES;
        
        [self.view addSubview:pickView];
    }
    

    UIPickerView的协议方法

    #pragma mark --- UIPickerViewDelegate
    /** 设置组件中每行的标题row:行 */
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        if (component == 0) {
            return _provinceArr[row];
        }else{
            NSInteger selectedRow = [pickerView selectedRowInComponent:0];
            NSArray *arr = [allArr objectAtIndex:selectedRow];
            
            return [arr objectAtIndex:row];
        }
    }
    /** 当选择某一个列中的某一行的时候会调用该方法 */
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
        //在拖动第 0 列行的时候, 要及时的刷新第 1 列的数据
        if (component == 0) {
            //如果滑动的是第 0 列, 刷新第 1 列
            //在执行完这句代码之后, 会重新计算第 1 列的行数, 重新加载第 1 列的标题内容
            [pickerView reloadComponent:1];//重新加载指定列的数据
            [pickerView selectRow:0 inComponent:1 animated:YES];
            //
            
            //重新加载数据
            //[pickerView reloadAllComponents];
        }
    }
    
    /** 设置组件的宽度 */
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
        if (component == 0) {
            return 100;
        }else{
            return 80;
        }
        
    }
    /** 设置组件中每行的高度 */
    - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
    {
        if (component == 0) {
            return 60;
        }else{
            return 60;
        }
    }
    
    //- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {}
    //- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{}
    
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    如果这篇文章对您有些许帮助 请给我点个心哦。

    相关文章

      网友评论

          本文标题:UIPickerView 使用详解

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