oc pickerView使用

//

//  CustomPickerView.m

//  Test

//

//  Created by Apple on 2020/7/8.

//  Copyright © 2020 网游二部一体机. All rights reserved.

//

#import "CustomPickerView.h"

@interface CustomPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong)UIPickerView *pickerView;

@property (nonatomic,strong)NSMutableArray<NSArray<NSArray *> *> *datas;

@property (nonatomic,assign)NSInteger currentIndex;

@end

@implementation CustomPickerView

-(NSMutableArray *)datas {

    if (!_datas) {

        _datas = [NSMutableArray arrayWithCapacity:2];

    }

    return _datas;

}

-(instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        [self setUI];

    }

    return self;

}

-(void)setUI {

    

    NSArray *array = @[@"1'",@"2'"];

    NSArray *array2 = @[@"1\"",@"2\""];

    NSArray *array3 = @[@"英寸",@"厘米"];

    [self.datas addObject:@[array,array2,array3]];

    

    NSArray *array4 = @[@"100",@"200"];

    NSArray *array5 = @[@"英寸",@"厘米"];

    [self.datas addObject:@[array4,array5]];

    

    self.currentIndex = 0;

    

    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];

    self.pickerView.dataSource = self;

    self.pickerView.delegate = self;

    

    [self addSubview:self.pickerView];

    

}

#pragma mark - UIPickerViewDataSource

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    return self.datas[_currentIndex].count;

}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    return self.datas[_currentIndex][component].count;

}

//列显示的数据

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component {

    NSString *title = self.datas[_currentIndex][component][row];

    return title;

}

#pragma mark - UIPickerViewDelegate

// 选中某一组的某一行

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (_currentIndex ==0) {

        if (component == 2 && row == 1) {//选中厘米 刷新数据

            _currentIndex = 1;

            [pickerView reloadAllComponents];

            [pickerView selectRow:1 inComponent:1 animated:NO];

        }

    }else {

        if (component == 1 && row == 0) {//选中英寸 刷新数据

            _currentIndex = 0;

            [pickerView reloadAllComponents];

            [pickerView selectRow:0 inComponent:2 animated:NO];

        }

    }

}

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_41620230

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值