iOS UIPickerView的简单使用和数组越界问题

一、简介

UIPickView是在iOS开发者经常用到的一种控件,经常用于城市选择,日期选择等。


二、初始化

        UIPickerView * PickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, screenW, 260)];
        PickerView.delegate = self;
        PickerView.dataSource = self;
        PickerView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:PickerView];


三、UIPickView

// 是否显示指示器,默认为NO
@property(nonatomic) BOOL showsSelectionIndicator;

// 列数
@property(nonatomic,readonly) NSInteger numberOfComponents;

// 返回某一列有多少行
- (NSInteger)numberOfRowsInComponent:(NSInteger)component;

// 返回某一列的大小
- (CGSize)rowSizeForComponent:(NSInteger)component;

// 某一列某一行的标题控件
- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;

// 重新加载所有列(UIPickView)
- (void)reloadAllComponents;

// 重新加载某一列
- (void)reloadComponent:(NSInteger)component;

// 选中到某一列某一行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
// 某一列的行数
- (NSInteger)selectedRowInComponent:(NSInteger)component;



四、UIPickerViewdataSource方法

// 一共有多少组(列)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

// 每列有多少行,传入的参数component是对应的列
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

五、UIPickerViewDelegate

// 列宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

// 行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;

// 返回字符串类型的文本标题
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

// 返回字符属性的字符串类型的文本标题,可改变返回的字符串颜色,但不能改变大小(测试过了)
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component;

// 返回view类型标题控件
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view;

// 选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;


六、代码示例-> .m文件,继承于UIPickView类(两列)

#import "PickKeyBoardView.h"

@interface PickKeyBoardView () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, retain) NSMutableArray * plarTypeArr;
@property (nonatomic, assign) NSInteger selectRowOfCom1;

@end

@implementation PickKeyBoardView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        
        _plarTypeArr = [self CityArr];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        
        _plarTypeArr = [self CityArr];
    }
    return self;
}

#pragma mark - UIPickerViewDataSource
// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

// 行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.plarTypeArr.count;
    }
    else {
        NSDictionary * dict = [self.plarTypeArr objectAtIndex:self.selectRowOfCom1];
        NSArray * playArr = [dict objectForKey:@"key2"];
        return playArr.count;
    }
}

// 列宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return screenW / 2;
}

// 标题
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    UILabel * label = (UILabel *)view;
    if (label == nil) {
        CGRect frame = CGRectMake(0.0, 0.0, screenW / 2, 40);
        label = [[UILabel alloc] initWithFrame:frame];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setFont:[UIFont systemFontOfSize:15.0f]];
    }
    
    if (component == 0) {
        label.backgroundColor = [UIColor greenColor];
        NSDictionary * dict = [self.plarTypeArr objectAtIndex:row];
        label.text = [dict valueForKey:@"key1"];
        return label;
    }else {
        label.backgroundColor = [UIColor yellowColor];
        NSDictionary * dict = [self.plarTypeArr objectAtIndex:self.selectRowOfCom1];
        NSArray * playArr = [dict valueForKey:@"key2"];
        label.text = [playArr objectAtIndex:row];
        return label;
    }
}

// 选中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        self.selectRowOfCom1 = row;
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:NO];
    }
}

- (NSArray *)CityArr {
    NSArray * array = @[@"福建省",@[@"泉州",@"福州",@"莆田",@"三明",@"厦门"],
                        @"广东省",@[@"广州",@"深圳",@"珠海",@"东莞",@"佛山"]];
    
    NSInteger i = 0;
    NSMutableArray * muArr = [[NSMutableArray alloc] init];
    while (i < array.count) {
        NSString * str = [array objectAtIndex:i];
        NSArray * arr = [array objectAtIndex:i+1];
        
        NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:str, @"key1", arr, @"key2", nil];
        [muArr addObject:dict];
        
        i = i + 2;
    }
    return muArr;
}

@end





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值