標籤:ui ios 控制項 uipackervi picker
貓貓分享,必須精品
素材代碼地址:http://download.csdn.net/detail/u013357243/8596279
原創文章,歡迎轉載。轉載請註明:翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents
先看 ##
代碼
//// NJViewController.m// 01-點菜//// Created by apple on 14-6-3.// Copyright (c) 2014年 heima. All rights reserved.//#import "NJViewController.h"@interface NJViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;/** * 隨機按鈕點擊事件 */- (IBAction)randomFood:(UIButton *)sender;/** * 所有食物 */@property (nonatomic, strong) NSArray *foods;/** * 水果 */@property (weak, nonatomic) IBOutlet UILabel *fruitLabel;/** * 主菜 */@property (weak, nonatomic) IBOutlet UILabel *stapleLabel;/** * 飲料 */@property (weak, nonatomic) IBOutlet UILabel *drinkLabel;@end@implementation NJViewController- (void)viewDidLoad{ [super viewDidLoad]; // 設定預設選中的內容// self.fruitLabel.text = self.foods[0][0];// self.stapleLabel.text = self.foods[1][0];// self.drinkLabel.text = self.foods[2][0];// [self pickerView:nil didSelectRow:0 inComponent:0];// [self pickerView:nil didSelectRow:0 inComponent:1];// [self pickerView:nil didSelectRow:0 inComponent:2]; for (int component = 0; component < self.foods.count; component++) { [self pickerView:nil didSelectRow:0 inComponent:component]; }}#pragma mark - UIPickerViewDataSource// 返回pickerView一共有多少列- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{// return 3; return self.foods.count;}// 返回pickerView的第component列有多少行- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{// return 4; // 1.擷取對應列的數組 NSArray *subFoods = self.foods[component]; // 2.返回對應列的行數 return subFoods.count;}#pragma mark - UIPickerViewDelegate// 返回第component列的第row行顯示什麼內容- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ // 1.擷取對應列的數組 NSArray *subFoods = self.foods[component]; // 2.擷取對應行的標題 NSString *name = subFoods[row]; return name;}// 當選中了pickerView的某一行的時候調用// 會將選中的列號和行號作為參數傳入// 只有通過手指選中某一行的時候才會調用- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{// NSLog(@"component = %d, row = %d", component, row); // 1.擷取對應列對應行的資料 NSString *name = self.foods[component][row];// NSLog(@"name = %@", name); // 2.判斷選擇的是哪一列, 根據列號設定對應的資料 if (0 == component) { // 水果 self.fruitLabel.text = name; }else if (1 == component) { // 主菜 self.stapleLabel.text = name; }else { // 飲料 self.drinkLabel.text = name; }}#pragma mark - 懶載入- (NSArray *)foods{ if (_foods == nil) { NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil]; _foods = [NSArray arrayWithContentsOfFile:fullPath]; } return _foods;}#pragma mark - 監聽按鈕點擊- (IBAction)randomFood:(UIButton *)sender { // 讓pickerView主動選中某一行 // 讓pickerView選中inComponent列的Row行// [self.pickerView selectRow:1 inComponent:0 animated:YES]; /* [self.pickerView selectRow: arc4random() % 12 inComponent:0 animated:YES]; [self.pickerView selectRow: arc4random() % 15 inComponent:1 animated:YES]; [self.pickerView selectRow: arc4random() % 10 inComponent:2 animated:YES]; */// [self.foods objectAtIndex:0]; == self.foods[0];// [self.foods[0] count]; /* // 根據每一列的元素個數產生隨機值 [self.pickerView selectRow: arc4random() % [self.foods[0] count] inComponent:0 animated:YES]; [self.pickerView selectRow: arc4random() % [self.foods[1] count] inComponent:1 animated:YES]; [self.pickerView selectRow: arc4random() % [self.foods[2] count] inComponent:2 animated:YES]; */ for (int component = 0; component < self.foods.count; component++) { // 擷取對應列的資料總數 int total = [self.foods[component] count]; // 根據每一列的總數產生隨機數(當前產生的隨機數) int randomNumber = arc4random() % total; // 擷取當前選中的行(上一次隨機後移動到的行) int oldRow = [self.pickerView selectedRowInComponent:0];// NSLog(@"oldRow = %d", oldRow); // 比較上一次的行號和當前產生的隨機數是否相同, 如果相同重建 while (oldRow == randomNumber) { randomNumber = arc4random() % total; } // 讓pickerview滾動到某一行 [self.pickerView selectRow: randomNumber inComponent:component animated:YES]; // 通過代碼選中某一行 [self pickerView:nil didSelectRow:randomNumber inComponent:component]; }}@end
ps:建立iOS交流學習群:304570962 可以加貓貓QQ:1764541256 或則znycat 讓我們一起努力學習吧。
翟乃玉的部落格
地址:http://blog.csdn.net/u013357243?viewmode=contents
(素材源碼)貓貓學IOS(二十)UI之UIPickerView_點菜系統