iOS UIPickerView

  • 普通展示
#import "ViewController.h"

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong)UIPickerView *pickerView;
@property(nonatomic,strong)NSArray *dataSouce;
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;

@end

@implementation ViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.pickerView];
//    默认选中数据
    //    [self pickerView:self.pickerView didSelectRow:0 inComponent:1];
    
//    每行的默认选中数据
    for (int i = 0; i < self.dataSouce.count; ++i) {
        [self.pickerView selectRow:1 inComponent:i animated:YES];
        [self pickerView:self.pickerView didSelectRow:1 inComponent:i];
    }
    
}
#pragma mark - public methods

#pragma mark - private methods

- (IBAction)random:(id)sender {
    
    // 遍历集合中的所有组
    for (int i = 0; i < self.dataSouce.count; ++i) {
        
        // 第i组里面的所有数据
        NSUInteger count = [self.dataSouce[i] count];
        
        // 生成随机数去选中
        u_int32_t ranNum = arc4random_uniform((int)count);
        
        // 获取第i组当前选中的行
        NSInteger selRowNum = [self.pickerView selectedRowInComponent:i];
        
        // 如果随机数与当前选中的行号一致,需要重新生成随机数
        while (selRowNum == ranNum) {
            ranNum = arc4random_uniform((int)count);
        }
        
        // 让pickerView选中数据
        [self.pickerView selectRow:ranNum inComponent:i animated:YES];
        
        // 将数据现实到label上
        [self pickerView:self.pickerView didSelectRow:ranNum inComponent:i];
    }
    
    
    
}

#pragma mark - getter && setter

#pragma mark - lazy loading

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 320)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
//        _pickerView.showsSelectionIndicator = YES;
    }
    return _pickerView;
    
}

- (NSArray *)dataSouce {
    if (!_dataSouce) {
//        _dataSouce = @[@[@"西瓜",@"西红柿",@"番茄"],@[@"",@"西红柿",@"番茄"],@[@"西瓜",@"西红柿",@"番茄"]];
        _dataSouce= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"01foods.plist" ofType:nil]];
    }
    return _dataSouce;
}

#pragma mark - dataSouce
//有几行
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.dataSouce.count;
}
//行中有几列
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self.dataSouce[component] count];
}
//列显示的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component {
    return self.dataSouce[component][row];
}

#pragma mark - delegate
// 选中某一组的某一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *selFood = self.dataSouce[component][row];
    NSLog(@"%@", selFood);
    switch (component) {
        case 0:
            self.label1.text = selFood;
            break;
            
        case 1:
            self.label2.text = selFood;
            break;
            
        case 2:
            self.label3.text = selFood;
            break;
            
        default:
            break;
    }
}

@end
  • 二级联动
#import "ViewController.h"

#import "YLModel.h"
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)UIPickerView *pickerView;

@property(nonatomic,strong)UILabel *leftLabel;
@property(nonatomic,strong)UILabel *rightLabel;

@property(nonatomic,strong)NSArray *dataSouce;
@property(nonatomic,strong)YLModel *selectModel;


@end

@implementation ViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.pickerView];
    [self.view addSubview:self.leftLabel];
    [self.view addSubview:self.rightLabel];

    [self pickerView:self.pickerView didSelectRow:0 inComponent:0];
    
}
#pragma mark - public methods

#pragma mark - private methods

#pragma mark - getter && setter

#pragma mark - lazy loading
- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 300)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        //        _pickerView.showsSelectionIndicator = YES;
    }
    return _pickerView;
    
}
- (NSArray *)dataSouce {
    if (!_dataSouce) {
//        plist 转 Array
        NSArray *dictArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"02cities.plist" ofType:nil]];
        
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:dictArr.count];
        for (NSDictionary *dict in dictArr) {
            YLModel *model =  [YLModel provinceWithDict:dict];
            [arrM addObject:model];
        }
        
        _dataSouce = arrM;
        
    }
    return _dataSouce;
    
}

- (UILabel *)leftLabel {
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 400, 100, 30)];
        
    }
    return _leftLabel;
    
}

- (UILabel *)rightLabel {
    if (!_rightLabel) {
        _rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 400, 100, 30)];
        
    }
    return _rightLabel;
    
}


#pragma mark - dataSouce
// Components
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
    
}
//Rows
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (0 == component) {
        return self.dataSouce.count;
    }
    else {
        //    选中的row在Component中
       NSInteger selectRow = [pickerView selectedRowInComponent:0];
       YLModel *model = self.dataSouce[selectRow];
        
        // 将新的省数据保存起来
        _selectModel = model;
        
        return model.cities.count;
        
    }
    
}
//title
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    
    if (0 == component) {
        YLModel *model =  self.dataSouce[row];
        return model.name;
    }
    else {
        return self.selectModel.cities[row];
    }
    
    
}
#pragma mark - delegate
//Select
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        [pickerView reloadComponent:1];
        
        [pickerView selectRow:0 inComponent:1 animated:YES];
    }
    
//  return  select Row In Component

   NSInteger selectProIndex =  [pickerView selectedRowInComponent:0];
   NSInteger selectCityIndex = [pickerView selectedRowInComponent:1];
    
    YLModel *model = self.dataSouce[selectProIndex];
    
    NSString *cityname = model.cities[selectCityIndex];
    
    self.leftLabel.text = model.name;
    self.rightLabel.text = cityname;
    
}

@end
  • 自定义pickerView
#import "ViewController.h"
#import "YLView.h"
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong)UIPickerView *pickerView;
@property(nonatomic,strong)NSArray *dataSouce;

@end

@implementation ViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.pickerView];
    
}
#pragma mark - public methods

#pragma mark - private methods

#pragma mark - getter && setter

#pragma mark - lazy loading
- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 170, self.view.bounds.size.width, 320)];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        //        _pickerView.showsSelectionIndicator = YES;
    }
    return _pickerView;
    
}

- (NSArray *)dataSouce {
    if (!_dataSouce) {
        
        _dataSouce= @[@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1"];
    }
    return _dataSouce;
}
#pragma mark - dataSouce
// Components
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
// row
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataSouce.count;
}
// custom view for row
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    NSString *imageName  = self.dataSouce[row];
    YLView *custcomView = [YLView YLView];
//     custcomView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 214);
    custcomView.imageName = imageName;
    return custcomView;
    
}
//❌❌❌
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component {
//    return self.dataSouce[component][row];
//}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 214;
}

@end

推荐阅读更多精彩内容