UI进阶-第01天
/**
摘要:
掌握UIPicderView的使用
掌握键盘工具条的使用
*/
一、UIPikcerView的使用
》打开官方文档 查看DatePicker/UIPikcerView在iOS6与iOS7的区别
查找至UserExperience — Guides — iOS 7 UI Transition Guide — Controls - Picker
"【案例: 点菜系统】"
//放个点菜系统的截图
》使用UIPikcerView控件实现点菜系统
》掌握UIPikcerView的代理与数据源与代理方法的使用,与TableView类比
(1)UITableView的每一行Cell是在数据源里,而UIPikcerView的每一行View是在代理里
(2)UIPickerView每一行长什么样有两个方法
//-(NSString *)pickerView: titleForRow: forComponent:直接返回一个字符串
//-(UIView *)pickerView: viewForRow: forComponent: reusingView:直接返回一个view
》掌握使用代理的【-(NSString *)pickerView:titleForRow:forComponent:】方法显示一组数据与显示多组数据
》加载foods.plist文件,显示多组数据
》监听每组选中的行,更改Label数据
(1)使用代理方法【-(void)pickerView:didSelectRow:inComponent:】
》实现默认选中每一组的第一行数据
(1)在viewDidLoad方法调用【-(void)pickerView:didSelectRow:inComponent:】实现
》实现随机选菜单
(1)实现Label数据的随机变更
(2)实现pickerView的数据随机变更
(3)每一组对应行的数据一定要不同上一次的行数据
/**
*获取旧行与新行,使用while循环,
//旧行
NSInteger oldRow = [self.pickerView selectedRowInComponent:i];
//随机新行
NSInteger newRow = arc4random_uniform((int)rows);
//新行与旧行相同,再随机,直到不两只
while (newRow == oldRow) {
newRow = arc4random_uniform((int)rows);
}
*/
ViewController.m
// 01.点菜系统
//
// Created by Yong Feng Guo on 14-12-16.
// Copyright (c) 2014年 Fung. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)NSArray *foods;
@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 *)foods{
if (!_foods) {
NSString *foodsPath = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];
_foods = [NSArray arrayWithContentsOfFile:foodsPath];
}
return _foods;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//默认显示每一组的第一行数据
NSInteger components = self.foods.count;
for (NSInteger i = 0; i<components; i++) {
[self pickerView:nil didSelectRow:0 inComponent:i];
}
}
#pragma mark -UIPickerView数据原
#pragma mark 多少组
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return self.foods.count;
}
#pragma mark 每组多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
NSArray *items = self.foods[component];
return items.count;
}
#pragma mark -UIPickerView数据代理
#pragma mark 对应组对应行的数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSArray *items = self.foods[component];
return items[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//获取对应组对应行的数据
NSString *food = self.foods[component][row];
switch (component) {
case 0:
self.fruitLabel.text = food;
break;
case 1:
self.mainFoodLabel.text = food;
break;
case 2:
self.drinkLabel.text = food;
break;
default:
break;
}
}
#pragma mark 随机菜单
- (IBAction)randomMenu:(id)sender {
NSInteger component = self.foods.count;
//生成每一组的随机数据
for (NSInteger i = 0; i < component; i++) {
NSArray *items = self.foods[i];
NSInteger rows = items.count;
//旧行
NSInteger oldRow = [self.pickerView selectedRowInComponent:i];
//随机新行
NSInteger newRow = arc4random_uniform((int)rows);
//新行与旧行相同,再随机,直到不两只
while (newRow == oldRow) {
newRow = arc4random_uniform((int)rows);
}
//pickerView没有变
[self pickerView:nil didSelectRow:newRow inComponent:i];
//改变pickerView的Cell
[self.pickerView selectRow:newRow inComponent:i animated:YES];
}
}
@end
"【出题】"
1>生成0.0-0.9的小数 arc4random_uniform(10) * 0.1)
2>生成0.00-0.99的小数 arc4random_uniform(100) * 0.01)
用乘效率会高些。