點擊「隨機」按鈕,pickerview 自動選取,下方數據同步刷新。

1. picker view 默認高度為 162,不可修改。
2. 顯示數據需設置數據源,有兩種方式:讓控制器成為數據源並遵守協議。
3. 實現數據源的兩個方法:返回總列數、返回每列的行數。
4. 透過代理告知它「某列的某行」顯示什麼數據(把代理設為控制器)。
5. 用懶加載加載所有食物數據。
6. 完成基本展示(列、行、內容)。
7. 自動更新選中的食物信息:用一個大 view 放六個 label,其中三個分別綁定水果、主菜、飲料屬性;透過 didSelectRow 代理方法監聽選中行,拿到列號與行號;在 viewDidLoad 中設置默認選中 [0][1],並用循環提高可擴展性。
8. 隨機功能:用 selectRow:inComponent:animated: 讓 pickerview 主動滾動到指定行;生成不超過總數的隨機數;注意避免兩次生成的隨機值相同(相同則列不會滾動),做法是把上一次的行號與新生成的隨機數比較,若相同則重新生成。
9. 還有一個坑:透過代碼選中某一行後,下方的數據刷新會失效,需要手動調用 didSelectRow 模擬一次選中。
項目結構與 storyboard 界面設置:


主控制器代碼:
#import "YYViewController.h" // 遵守數據源和代理協議 @interface YYViewController ()@property (strong, nonatomic) IBOutlet UILabel *fruitLab; // 水果 @property (strong, nonatomic) IBOutlet UILabel *stapleLab; // 主菜 @property (strong, nonatomic) IBOutlet UILabel *drinkLab; // 飲料 @property (nonatomic, strong) NSArray *foods; // 保存所有數據 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView; - (IBAction)randomFood:(id)sender; @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; // 設置下方數據的初始顯示 for (int component = 0; component < self.foods.count; component++) { [self pickerView:nil didSelectRow:0 inComponent:component]; } } // 懶加載食物數據 -(NSArray *)foods { if (_foods == nil) { NSString *fullpath = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil]; _foods = [NSArray arrayWithContentsOfFile:fullpath]; } return _foods; } // 隨機按鈕點擊事件 - (IBAction)randomFood:(id)sender { for (int component = 0; component < self.foods.count; component++) { int total = [self.foods[component] count]; // 當前列元素個數 int randomNumber = arc4random() % total; // 生成隨機數 int oldRow = [self.pickerView selectedRowInComponent:component]; // 上一次選中的行 // 若與上一次相同則重新生成,保證列會滾動 while (oldRow == randomNumber) { randomNumber = arc4random() % total; } [self.pickerView selectRow:randomNumber inComponent:component animated:YES]; // 模擬手動選中,觸發下方數據刷新 [self pickerView:nil didSelectRow:randomNumber inComponent:component]; } } // 一共多少列 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return self.foods.count; } // 每列多少行 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSArray *arayM = self.foods[component]; return arayM.count; } // 每列每行顯示的數據 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { NSArray *arayM = self.foods[component]; return arayM[row]; } // 選中某一行時調用,刷新下方數據 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSString *name = self.foods[component][row]; if (0 == component) { self.fruitLab.text = name; } else if (1 == component) { self.stapleLab.text = name; } else { self.drinkLab.text = name; } } // 隱藏狀態欄 -(BOOL)prefersStatusBarHidden { return YES; } @end
請注意代碼裡為什麼用 [self.foods[0] count] 而不是 self.foods[0].count。原因在於:self.foods[0] 等價於 [self.foods objectAtIndex:0],返回的是一個 id 類型的萬能指針,它的真實類型要到運行時才能確定,編譯器無法直接推斷出 count,因此不能用點語法,只能像 [self.foods[0] count] 這樣顯式發消息。