您的位置:首页 > 产品设计 > UI/UE

206,UIPickView类

2016-01-13 20:03 387 查看




ViewController.m:

#import "ViewController.h"

#define kFontSize 20

@interface
ViewController () <UIPickerViewDataSource,UIPickerViewDelegate>

//数据

@property (nonatomic,strong)
NSArray *foodsList;

//控件

@property (nonatomic,strong)
UIButton *randBtn;

@property (nonatomic,strong)
UIPickerView *pickView;

@property (nonatomic,strong)
UILabel *fruitLabel;

@property (nonatomic,strong)
UILabel *mainLabel;

@property (nonatomic,strong)
UILabel *drinksLabel;

@end

@implementation ViewController

//获取数据

- (NSArray *)foodsList{

if (_foodsList ==
nil) {

_foodsList = [NSArray
arrayWithContentsOfFile:[[NSBundle
mainBundle] pathForResource:@"foods"
ofType:@".plist"]];

}

return
_foodsList;

}

//创建控件,并设置属性与方法

- (UIButton *)randBtn{

if (_randBtn ==
nil) {

_randBtn = [[UIButton
alloc] initWithFrame:CGRectMake(10,
0, 44,
44) ];

[_randBtn
setTitle:@"随机"
forState:UIControlStateNormal];

[_randBtn
setTitleColor:[UIColor
blueColor] forState:UIControlStateNormal];

[_randBtn
setTitleColor:[UIColor
grayColor] forState:UIControlStateHighlighted];

[_randBtn
addTarget:self
action:@selector(random)
forControlEvents:UIControlEventTouchUpInside];

}

return
_randBtn;

}

- (UIPickerView *)pickView{

if (_pickView ==
nil) {

_pickView = [[UIPickerView
alloc]initWithFrame:CGRectMake(0,
80, self.view.bounds.size.width,100)];

_pickView.dataSource =
self;

_pickView.delegate =
self;

[self.view
addSubview:_pickView];

}

return
_pickView;

}

- (UILabel *)fruitLabel{

if (_fruitLabel ==
nil) {

_fruitLabel = [[UILabel
alloc] initWithFrame:CGRectMake(70,
291,
100, kFontSize)];

_fruitLabel.font = [UIFont
systemFontOfSize:kFontSize];

[self.view
addSubview:_fruitLabel];

}

return
_fruitLabel;

}

- (UILabel *)mainLabel{

if (_mainLabel ==
nil) {

_mainLabel = [[UILabel
alloc] initWithFrame:CGRectMake(70,
321,
100, kFontSize)];

_mainLabel.font = [UIFont
systemFontOfSize:kFontSize];

[self.view
addSubview:_mainLabel];

}

return
_mainLabel;

}

- (UILabel *)drinksLabel{

if (_drinksLabel ==
nil) {

_drinksLabel = [[UILabel
alloc] initWithFrame:CGRectMake(70,
351,
100, kFontSize)];

_drinksLabel.font = [UIFont
systemFontOfSize:kFontSize];

[self.view
addSubview:_drinksLabel];

}

return
_drinksLabel;

}

- (void)createUI{

//上部分控件

UIView *view = [[UIView
alloc]initWithFrame:CGRectMake(0,
20, self.view.bounds.size.width,44)];

view.backgroundColor = [UIColor
grayColor];

[self.view
addSubview:view];

UILabel *titleLabel = [[UILabel
alloc] init];

titleLabel.text =
@"点菜系统";

titleLabel.font = [UIFont
systemFontOfSize:25];

titleLabel.textColor = [UIColor
redColor];

[titleLabel sizeToFit];

titleLabel.center =
CGPointMake(view.center.x,
20);

[view addSubview:titleLabel];

[view addSubview:self.randBtn];

//中部控件

[self pickView];

//下部分控件

UILabel *fruitTip = [[UILabel
alloc] init];

fruitTip.text =
@"水果:";

fruitTip.font = [UIFont
systemFontOfSize:kFontSize];

[fruitTip sizeToFit];

fruitTip.center =
CGPointMake(40,
300);

[self.view
addSubview:fruitTip];

UILabel *mainTip = [[UILabel
alloc] init];

mainTip.text =
@"主食:";

mainTip.font = [UIFont
systemFontOfSize:kFontSize];

[mainTip sizeToFit];

mainTip.center =
CGPointMake(40,
330);

[self.view
addSubview:mainTip];

UILabel *drinksTip = [[UILabel
alloc] init];

drinksTip.text =
@"饮料:";

drinksTip.font = [UIFont
systemFontOfSize:kFontSize];

[drinksTip sizeToFit];

drinksTip.center =
CGPointMake(40,
360);

[self.view
addSubview:drinksTip];

[self
fruitLabel];

[self
mainLabel];

[self
drinksLabel];

}

- (void)viewDidLoad {

[super
viewDidLoad];

//界面搭建

[self createUI];

//初始化数据

for (int i =
0; i < 3; i ++) {

[self
pickerView:self.pickView
didSelectRow:0
inComponent:i];

}

}

#pragma mark - 设置数据源

//返回多少个组件(列数)

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

return
self.foodsList.count;

}

//返回每一个组件的行总数

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

NSArray *arr =
self.foodsList[component];

return arr.count;

}

////使用字符串显示内容

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

// NSArray *arr = self.foodsList[component];

// return arr[row];

//}

//使用控件显示内容

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable
UIView *)view {

UILabel *label =
nil;

//创建或获取可重用的view

if (label == nil) {

label = [[UILabel
alloc] init];

}else{

label = (UILabel *)view;

}

//设置数据

NSArray *arr =
self.foodsList[component];

label.text = arr[row];

label.textAlignment =
NSTextAlignmentCenter;

if (component ==
0) {

label.textColor = [UIColor
redColor];

}else if(component ==
1){

label.textColor = [UIColor
blueColor];

}else{

label.textColor = [UIColor
greenColor];

}

return label;

}

//滚动选择停止后,执行该方法

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

NSArray *arr =
self.foodsList[component];

if (component ==
0) {

self.fruitLabel.text = arr[row];

}else if(component ==
1){

self.mainLabel.text = arr[row];

}else{

self.drinksLabel.text = arr[row];

}

}

////设置列宽

//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

// return 121;

//}

//

////设置行高

//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

// return 100;

//}

- (void)random{

int component = (int)self.foodsList.count;

for (int i =
0; i < component; i ++) {

NSArray *arr =
self.foodsList[i];

int oldRow = [self.pickView
selectedRowInComponent:i];

int newRow =
arc4random_uniform(arr.count);

while (oldRow == newRow) {

newRow = arc4random_uniform(arr.count);

}

[self
pickerView:self
didSelectRow:newRow inComponent:i];

[self.pickView
selectRow:newRow inComponent:i
animated:YES];

}

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: