iOSiOS模块详解iOS控件使用大全

iOS-UIPickerView详解

2016-06-14  本文已影响15957人  xx_cc

iOS-UIPickerView详解

// pickView初始化并设置其大小,如果不设置其大小,默认大小为 320 * 216。

UIPickerView *pickView = [[UIPickerView alloc]initWithFrame:self.view.frame];

// 显示选中指示器,有一个透明的覆盖在选中航,默认是NO,iOS7 之后总是显示选中指示器,设置这个属性没有影响。

pickView.showsSelectionIndicator = YES;

//在iOS 7之后可以自定义选择器视图的背景颜色改变其backgroundColor

pickView.backgroundColor = [UIColor grayColor];

//设置pickVIew的透明度

pickView.alpha = 0.7;

//获取pickView的列数,只读属性

pickView.numberOfComponents;

//获取某一列的行数

NSInteger rowNum = [pickView numberOfRowsInComponent:0];

//获取某一列行的Size

CGSize rowSize = [pickView rowSizeForComponent:0];

//返回第component列,第row行的控件。
//一般用代理的pickerView:viewForRow:forComponent:reusingView:方法。

UIView *view = [pickView viewForRow:0 forComponent:0];

// 更新某一列 和 更新全部

[pickView reloadComponent:0];
[pickView reloadAllComponents];

//动画效果跳到选中某一列的某一行

[pickView selectRow:0 inComponent:0 animated:YES];

//返回某一列的选中行,-1表示没有选中行

[pickView selectedRowInComponent:0];  

UIPickerView的代理方法

UIPickerView的代理方法有两个UIPickerViewDelegate和UIPickerViewDataSource

UIPickerViewDataSource 数据源代理方法设置UIPickerView的行数与列数。

@protocol UIPickerViewDataSource<NSObject>
@required

// 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

// 返回每列的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
@end

!以上这两个方法是代理必须实现的。

UIPickerViewDelegate代理方法设置UIPickerView的内容。

@protocol UIPickerViewDelegate<NSObject>@optional

// 反回pickerView的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED;

// 返回pickerView的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED;
  
// 返回pickerView 每行的内容
- (nullableNSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED;

- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component 

// 返回pickerView 每行的view 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullableUIView *)view __TVOS_PROHIBITED;

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

!以上方法为选择实现

了解这些以后 我们做一个简单的UIPickerView 就不成问题了

//
//  ViewController.m
//  01-点菜系统
#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic , strong)NSArray *foodsData;
@property (weak, nonatomic) IBOutlet UILabel *fruitLabel;
@property (weak, nonatomic) IBOutlet UILabel *mainFoodLabel;
@property (weak, nonatomic) IBOutlet UILabel *drinkLabel;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@end

@implementation ViewController

// 数据数组的懒加载
-(NSArray *)foodsData
{
    if (_foodsData == nil) {
        _foodsData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil]];
    }
    return _foodsData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.fruitLabel.text = self.foodsData[0][0];
    self.mainFoodLabel.text = self.foodsData[1][0];
    self.drinkLabel.text = self.foodsData[2][0];
    
}

#pragma mark UIPickerViewDataSource 数据源方法
// 返回多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.foodsData.count;
    
}

// 返回多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray *items = self.foodsData[component];
    return items.count;
    
}

#pragma mark UIPickerViewDelegate 代理方法

// 返回每行的标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.foodsData[component][row];
}
// 选中行显示在label上
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSArray *items = self.foodsData[component];
    if (component == 0) {
        self.fruitLabel.text = items[row];
    }else if (component == 1){
        self.mainFoodLabel.text = items[row];
    }else{
        self.drinkLabel.text = items[row];
    }
}
// 随机按钮点击事件
- (IBAction)randomNumber {
    int com = (int)self.foodsData.count;
    
    for (int i = 0; i < com; i++) {
        int oldrow = (int)[self.pickerView selectedRowInComponent:i];
        
        NSArray *items = self.foodsData[i];
        
        int num = (int)items.count;
        
        int randomNum = oldrow;
        
        while (oldrow == randomNum) {
            randomNum = arc4random() %num;
        }
        [self.pickerView selectRow:randomNum inComponent:i animated:YES];
        [self pickerView:self.pickerView didSelectRow:randomNum inComponent:i];
    }
    
}
@end

最后,效果图

UIPickerView简单实用效果图

最后附上一个稍微复杂一点的UIPickerView使用练习代码代码下载

✨本文借鉴了很多前辈的文章,如果有不对的地方请指正,欢迎大家一起交流学习 xx_cc

上一篇下一篇

猜你喜欢

热点阅读