ios中 pickerView的用法

今天是一个特殊的日子(Mac pro 敲的 爽。。。 昨天到的)

ios中 pickerView的用法

//
// QRViewController.m// #import "QRViewController.h" @interface QRViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *fruitLable;//水果
@property (weak, nonatomic) IBOutlet UILabel *mainLabel;//主菜
@property (weak, nonatomic) IBOutlet UILabel *drinkLabel;//饮料
@property (nonatomic,strong) NSArray *foods;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
- (IBAction)ranDom; @end @implementation QRViewController - (void)viewDidLoad {
[super viewDidLoad];
for (int i=; i<self.foods.count; i++) {
[self pickerView:nil didSelectRow: inComponent:i];
}
//[self pickerView:nil didSelectRow:0 inComponent:0];
//[self pickerView:nil didSelectRow:0 inComponent:1];
//[self pickerView:nil didSelectRow:0 inComponent:2];
}
/**
*懒加载数据
*/
- (NSArray *)foods
{
if(_foods==nil){
NSString *path=[[NSBundle mainBundle] pathForResource:@"foods" ofType:@"plist"];
NSArray *arraylist=[NSArray arrayWithContentsOfFile:path];
_foods=arraylist;
}
return _foods;
}
#pragma mark -数据源方法
/**
*一共多少列
*/
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return self.foods.count;
}
/**
* 某一列显示多少行
*/
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *subfoods=self.foods[component];
return subfoods.count;
} #pragma mark - 代理方法
/**
*某一列中的某一行显示的数据
*/
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return self.foods[component][row];
}
/**
*选中某一行一列
*/
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component==){//水果
self.fruitLable.text=self.foods[component][row];
}else if(component==){//主菜
self.mainLabel.text=self.foods[component][row];
}else if(component==){//饮料
self.drinkLabel.text=self.foods[component][row];
}
}
/**
*随机生成一个
*/
- (IBAction)ranDom {
for (int i=; i<self.foods.count; i++) {
//每一行的总长度
int count=[self.foods[i] count];
//生成一个随机数
int row=arc4random()%count;
//设置pickerView选中的列的行
[self.pickerView selectRow:row inComponent:i animated:YES];
//设置Label的文字
[self pickerView:nil didSelectRow:row inComponent:i];
} } @end
上一篇:centos7 redmine安装过程(转载)


下一篇:不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)