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
相关文章推荐
- UI课程(导航栏navigationItem)
- 自定义本地推送通知(UILocalNotification) 星期和时间
- LeetCode N-Queens
- UIButton设置图片和文字
- iOS开发之:dispatch_async 与 dispatch_get_global_queue 的使用方法
- NGUI:浅谈NGUI中UISprite和UITexture
- UIDynamic介绍
- QUESTION 56 Automatic Shared Memory Management is disabled for your database instance. You realize t
- 46: Which two statements are true about setting the FAST_START_MTTR_TARGET initialization parameter
- iOS开发之UIGestureRecognizer
- 62. Unique Paths && 63. Unique Paths II
- apue高级IO
- Puer是一个可以实时编辑刷新的前端服务器
- 简单谈谈select, iocp, epoll,kqueue及各种I/O复用机制
- 如何在虚拟机中安装Bluestacks
- UICollectionView的header悬停
- UI控件 简单
- EasyUI常用控件禁用方法
- 阻塞队列--LinkedBlockingQueue
- easyui datagrid 搜索功能