標籤:style blog http io color ar os 使用 sp
一、實現效果
說明:點擊隨機按鈕,能夠自動選取,下方資料自動重新整理。
二、實現思路
1.picker view的有預設高度為162,不可修改。2.顯示資料,需要設定資料來源,也有兩種方式(成為資料來源,遵守協議)3.實現資料來源裡面的兩個方法1)返回一共有多少列2)在這一列中一共有多少行4.通過代理告訴它那一列的哪一行顯示哪些資料(設定其代理為控制器)5.使用懶載入,載入所有的食物6.完成基本資料的展示(列,行,內容)7.自動更新選中的食物資訊。(使用一個大的view,上面放6個label)1)給3個lab賦值,添加三個屬性(水果,主菜,飲料)2)監聽選中了哪一行(監聽有兩種思想,一個是代理,一個是通知),先查看有沒有代理的方法(didselectRow)這個方法當選中了某一行的的時候調用,會將選中的列號和行號當做參數傳入進去。能夠擷取到對應的列號和行號。3)完成選中時調用的監聽方法4)在viewdidload裡面設定預設選中的內容,設定為[0][1]5)提高可擴充性(手動的調用那幾行-使用一個for迴圈)8.隨機功能的實現1)怎麼讓代碼選中某一行(selectrow),調用該方法可以指定讓它滾動到那一列的哪一行2)實現頭部的功能(使用一個大的uiview,裡面放兩個子控制項)3)設定高度44,怎麼讓隨機按鈕的位置置中?可以設定它的高度為44,最大的Y值為64。4)設定隨機按鈕的點擊事件randomFood,讓pickerview主動選中某一行。5)產生隨機數的方法(產生隨機數的限制,不超過當前的總數)6)缺點,將來資料改變之後,會報錯(模於幾)[self.foods[0] count]?為什麼不用簡寫 點文法?(切記要記住)7)隨機數的處理不嚴謹,有的時候產生的隨機數可能是相等的,那麼這樣的話列就不會滾動,擷取到對應列的資料總數,如何拿到上一次產生的隨機值(也就是當前選中的行),比較上一次的行號和當前產生的隨機數是否相同,如果相同則重寫產生9.解決另外一個問題,下面的資料隨機重新整理失效了,通過代碼選中某一行。
三、實現程式碼範例1.項目文檔結構和storyboard檔案storyboard檔案大的介面設定:2.程式碼範例主控制器檔案代碼:
1 // 2 // YYViewController.m 3 // 06-簡單選菜系統的實現 4 // 5 // Created by apple on 14-6-5. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 //遵守資料來源和代理協議 12 @interface YYViewController ()<UIPickerViewDataSource,UIPickerViewDelegate> 13 /** 14 * 水果 15 */ 16 @property (strong, nonatomic) IBOutlet UILabel *fruitLab; 17 /** 18 * 主菜 19 */ 20 @property (strong, nonatomic) IBOutlet UILabel *stapleLab; 21 /** 22 * 飲料 23 */ 24 @property (strong, nonatomic) IBOutlet UILabel *drinkLab; 25 /** 26 * 儲存所有的資料 27 */ 28 @property(nonatomic,strong)NSArray *foods; 29 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView; 30 - (IBAction)randomFood:(id)sender; 31 32 @end 33 34 @implementation YYViewController 35 36 - (void)viewDidLoad 37 { 38 [super viewDidLoad]; 39 40 //在這裡設定下方資料重新整理部分的初始顯示 41 for (int component = 0; component<self.foods.count; component++) { 42 [self pickerView:nil didSelectRow:0 inComponent:component]; 43 } 44 } 45 46 #pragma mark-使用懶載入,把資料資訊載入進來 47 -(NSArray *)foods 48 { 49 if (_foods==nil) { 50 NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil]; 51 NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath]; 52 _foods=arrayM; 53 } 54 return _foods; 55 } 56 57 #pragma mark-處理隨機按鈕的點擊事件 58 - (IBAction)randomFood:(id)sender { 59 60 // 讓pickerView主動選中某一行 61 // 讓pickerView選中inComponent列的Row行 62 // [self.pickerView selectRow:1 inComponent:0 animated:YES]; 63 64 /* 65 [self.pickerView selectRow: arc4random() % 12 inComponent:0 animated:YES]; 66 [self.pickerView selectRow: arc4random() % 15 inComponent:1 animated:YES]; 67 [self.pickerView selectRow: arc4random() % 10 inComponent:2 animated:YES]; 68 */ 69 70 // [self.foods objectAtIndex:0]; == self.foods[0]; 71 // [self.foods[0] count]; 72 73 /* 74 // 根據每一列的元素個數產生隨機值 75 [self.pickerView selectRow: arc4random() % [self.foods[0] count] inComponent:0 animated:YES]; 76 [self.pickerView selectRow: arc4random() % [self.foods[1] count] inComponent:1 animated:YES]; 77 [self.pickerView selectRow: arc4random() % [self.foods[2] count] inComponent:2 animated:YES]; 78 */ 79 80 //設定一個隨機數 81 for (int component=0; component<self.foods.count; component++) { 82 //擷取當前列對應的資料元素的個數 83 int total=[self.foods[component] count]; 84 //根據每一列的總數產生隨機數(當前產生的隨機數) 85 int randomNumber=arc4random()%total; 86 87 //擷取當前選中的行(上一次隨機後移動到的行) 88 int oldRow=[self.pickerView selectedRowInComponent:0]; 89 90 //比較上一次的行號和當前產生的隨機數是否相同,如果相同的話則重建 91 while (oldRow==randomNumber) { 92 randomNumber=arc4random()%total; 93 } 94 95 //讓pickerview滾動到指定的某一行 96 [self.pickerView selectRow:randomNumber inComponent:component animated:YES]; 97 //類比,通過代碼選中某一行 98 [self pickerView:nil didSelectRow:randomNumber inComponent:component]; 99 }100 }101 102 #pragma mark- 設定資料103 //一共多少列104 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView105 {106 return self.foods.count;107 }108 109 //每列對應多少行110 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component111 {112 //1.擷取當前的列113 NSArray *arayM= self.foods[component];114 //2.返回當前列對應的行數115 return arayM.count;116 }117 118 //每列每行對應顯示的資料是什麼119 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component120 {121 //1.擷取當前的列122 NSArray *arayM= self.foods[component];123 //2.擷取當前列對應的行的資料124 NSString *name=arayM[row];125 return name;126 }127 128 #pragma mark-設定下方的資料重新整理129 // 當選中了pickerView的某一行的時候調用130 // 會將選中的列號和行號作為參數傳入131 // 只有通過手指選中某一行的時候才會調用132 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component133 {134 //擷取對應列,對應行的資料135 NSString *name=self.foods[component][row];136 //賦值137 if (0==component) {138 self.fruitLab.text=name;139 }else if(1==component)140 {141 self.stapleLab.text=name;142 }else143 self.drinkLab.text=name;144 }145 146 #pragma mark-隱藏狀態列147 -(BOOL)prefersStatusBarHidden148 {149 return YES;150 }151 @end
四、重要補充
請注意在代碼實現中為什麼使用 [self.foods[0] count]; 而不是直接使用點文法self.foods[0].count取值。
[self.foods objectAtIndex:0]; == self.foods[0];//這兩句的效果等價,而self調用objectAtIndex:0這個方法,返回的是一個id類型的萬能指標,它的真實類型要到實際啟動並執行時候才能檢測得到,因此不能直接使用self.foods[0].count。
iOS開發UI篇—使用picker View控制項完成一個簡單的選餐應用