Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> UIPickerView的使用

UIPickerView的使用

編輯:關於Android編程

UIPickerView功能與UIDatePicker類似

初始化實例時,通常只需要設置原點坐標,不需要設置寬高(默認寬高為:frame = (0 0; 320 216))

區別在於:

1、UIPickerView需要自定義實現數據類型(包括:數據源,列數等)

  1. @interfaceViewController()
  2.  
  3. @property(nonatomic,strong)NSDictionary*sourceDict;
  4. @property(nonatomic,strong)NSString*rowTitleFirst;
  5. @property(nonatomic,strong)NSString*rowTitleSecond;
  6. @property(nonatomic,strong)NSString*rowTitleThird;
  7.  
  8. @property(nonatomic,strong)UITextField*textfield;
  9.  
  10. @end
  11. UIPickerView*pickerView=[[UIPickerViewalloc]init];
  12. //[self.viewaddSubview:pickerView];
  13. pickerView.backgroundColor=[UIColororangeColor];
  14. NSLog(@"pickerView%@",pickerView);
  15. /*
  16. 設置代理
  17. 1包括代理對象,及數據源代理對象
  18. 2實現代理方法的對象
  19. 3添加協議
  20. 4實現代理方法
  21.  
  22. 注意事項
  23. 1、兩種代理必須同時實現
  24. 2、標題顯示設置三選一,只設置其中一個方法即可
  25. 2-1、-(nullableNSString*)pickerView:(UIPickerView*)pickerViewtitleForRow:(NSInteger)rowforComponent:(NSInteger)component
  26. 2-2、-(nullableNSAttributedString*)pickerView:(UIPickerView*)pickerViewattributedTitleForRow:(NSInteger)rowforComponent:(NSInteger)component
  27. 2-3、-(UIView*)pickerView:(UIPickerView*)pickerViewviewForRow:(NSInteger)rowforComponent:(NSInteger)componentreusingView:(nullableUIView*)view
  28. */
  29. pickerView.delegate=self;
  30. pickerView.dataSource=self;
  31. //設置數據源
  32. NSArray*meizhouArray=@[@"梅縣",@"五華縣",@"豐順縣",@"大埔縣",@"蕉嶺縣",@"興寧市",@"梅江區"];
  33. NSArray*guangzhouArray=@[@"天河區",@"白雲區",@"荔灣區",@"番禺區",@"海珠區",@"花都區",@"從化區",@"增城區"];
  34. NSArray*shenzhenArray=@[@"龍華新區",@"大鵬新區",@"福田區",@"羅湖區",@"寶安區",@"龍崗區",@"南山區"];
  35. NSDictionary*guangdongCityDict=[NSDictionarydictionaryWithObjectsAndKeys:shenzhenArray,@"深圳市",guangzhouArray,@"廣州市",meizhouArray,@"梅州市",nilnil];
  36. NSDictionary*guangxiCityDict=[NSDictionarydictionaryWithObjectsAndKeys:@[],@"柳州市",@[],@"桂林市",nilnil];
  37. self.sourceDict=[NSDictionarydictionaryWithObjectsAndKeys:guangdongCityDict,@"廣東省",guangxiCityDict,@"廣西省",nilnil];
  38. //設置默認值
  39. self.rowTitleFirst=@"廣東省";
  40. self.rowTitleSecond=@"深圳市";
  41. [pickerViewselectRow:0inComponent:0animated:NO];
  42. [pickerViewselectRow:0inComponent:1animated:NO];
  43. [pickerViewselectRow:3inComponent:2animated:NO];
  44. //刷新數據
  45. [pickerViewreloadAllComponents];
  46. //通過textfield來使用
  47. self.textfield=[[UITextFieldalloc]initWithFrame:CGRectMake(10.0,50.0,(CGRectGetWidth(self.view.bounds)-10.0*2),40.0)];
  48. [self.viewaddSubview:self.textfield];
  49. self.textfield.backgroundColor=[UIColoryellowColor];
  50. self.textfield.textColor=[UIColorredColor];
  51. self.textfield.placeholder=@"請選擇地址(省市區)";
  52. //添加鍵盤上方的子視圖
  53. UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom];
  54. button.frame=CGRectMake(0.0,0.0,CGRectGetWidth(self.view.bounds),40.0);
  55. button.backgroundColor=[UIColorgreenColor];
  56. [buttonsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
  57. [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateHighlighted];
  58. [buttonsetTitle:@"隱藏鍵盤"forState:UIControlStateNormal];
  59. [buttonaddTarget:selfaction:@selector(hiddenKeyboard:)forControlEvents:UIControlEventTouchUpInside];
  60. self.textfield.inputAccessoryView=button;
  61. //輸入源改成地址選擇視圖
  62. self.textfield.inputView=pickerView;
  63. //UIPickerViewDelegate
  64.  
  65. -(CGFloat)pickerView:(UIPickerView*)pickerViewwidthForComponent:(NSInteger)component
  66. {
  67. //設置列的寬度
  68. return100.0;
  69. }
  70.  
  71. -(CGFloat)pickerView:(UIPickerView*)pickerViewrowHeightForComponent:(NSInteger)component
  72. {
  73. //設置列中的每行的高度
  74. return40.0;
  75. }
  76.  
  77. -(nullableNSString*)pickerView:(UIPickerView*)pickerViewtitleForRow:(NSInteger)rowforComponent:(NSInteger)component
  78. {
  79. //設置列中的每行的顯示標題NSString
  80.  
  81. //設置每列每行的標題
  82. NSArray*keyArray=self.sourceDict.allKeys;
  83. if(0==component)
  84. {
  85. NSString*title=keyArray[row];
  86. returntitle;
  87. }
  88. elseif(1==component)
  89. {
  90. NSDictionary*cityDict=self.sourceDict[self.rowTitleFirst];
  91. NSArray*cityArray=cityDict.allKeys;
  92. NSString*title=cityArray[row];
  93. returntitle;
  94. }
  95. elseif(2==component)
  96. {
  97. NSDictionary*areaDict=self.sourceDict[self.rowTitleFirst];
  98. NSArray*areaArray=areaDict[self.rowTitleSecond];
  99. NSString*title=areaArray[row];
  100. returntitle;
  101. }
  102.  
  103. returnnil;
  104. }
  105.  
  106. //-(nullableNSAttributedString*)pickerView:(UIPickerView*)pickerViewattributedTitleForRow:(NSInteger)rowforComponent:(NSInteger)component
  107. //{
  108. ////設置列中的每行的顯示標題NSAttributedString
  109. //returnnil;
  110. //}
  111.  
  112. //-(UIView*)pickerView:(UIPickerView*)pickerViewviewForRow:(NSInteger)rowforComponent:(NSInteger)componentreusingView:(nullableUIView*)view
  113. //{
  114. ////設置列中的每行的自定義視圖
  115. //returnnil;
  116. //}
  117.  
  118. -(void)pickerView:(UIPickerView*)pickerViewdidSelectRow:(NSInteger)rowinComponent:(NSInteger)component
  119. {
  120. //獲取列中選中的某一行
  121.  
  122. NSLog(@"component=%ld,row=%ld",component,row);
  123.  
  124. NSArray*keyArray=self.sourceDict.allKeys;
  125. if(0==component)
  126. {
  127. NSString*keyFirst=keyArray[row];
  128. //設置第一列的值,即key1
  129. self.rowTitleFirst=keyFirst;
  130. //當第一列改變時,第二列的值應該跟著改變,即key2,設置為默認第二列的第一個值
  131. NSDictionary*cityDict=self.sourceDict[self.rowTitleFirst];
  132. NSArray*cityArray=cityDict.allKeys;
  133. if(cityArray&&0 {
  134. NSString*keySecond=cityArray[0];
  135. self.rowTitleSecond=keySecond;
  136. }
  137. //設置第三個標題,當第二列改變時,第三wfhr值路著改變
  138. NSArray*areaArray=cityDict[self.rowTitleSecond];
  139. if(areaArray&&0 {
  140. NSString*keyThird=areaArray[0];
  141. self.rowTitleThird=keyThird;
  142. }
  143. else
  144. {
  145. //沒有第三列時,將第三列的默認標題設置為空
  146. self.rowTitleThird=nil;
  147. }
  148.  
  149. //重新刷新第2列的數據,且設置顯示為第一行
  150. [pickerViewreloadComponent:1];
  151. [pickerViewselectRow:0inComponent:1animated:YES];
  152.  
  153. //重新刷新第3列的數據,且設置顯示為第一行
  154. [pickerViewreloadComponent:2];
  155. [pickerViewselectRow:0inComponent:2animated:YES];
  156. }
  157. elseif(1==component)
  158. {
  159. //設置第二個標題
  160. NSDictionary*cityDict=self.sourceDict[self.rowTitleFirst];
  161. NSArray*cityArray=cityDict.allKeys;
  162. NSString*keySecond=cityArray[row];
  163. self.rowTitleSecond=keySecond;
  164.  
  165. //設置第三個標題,默認第一行
  166. NSArray*areaArray=cityDict[self.rowTitleSecond];
  167. if(areaArray&&0 {
  168. NSString*keyThird=areaArray[0];
  169. self.rowTitleThird=keyThird;
  170. }
  171.  
  172.  
  173. //重新刷新第3列的數據,且設置顯示為第一行
  174. [pickerViewreloadComponent:2];
  175. [pickerViewselectRow:0inComponent:2animated:YES];
  176. }
  177. elseif(2==component)
  178. {
  179. //設置第三個標題
  180. NSDictionary*cityDict=self.sourceDict[self.rowTitleFirst];
  181. NSArray*areaArray=cityDict[self.rowTitleSecond];
  182. if(areaArray&&0 {
  183. NSString*keyThird=areaArray[row];
  184. self.rowTitleThird=keyThird;
  185. }
  186. }
  187.  
  188. NSString*text=[NSStringstringWithFormat:@"%@%@%@",self.rowTitleFirst,self.rowTitleSecond,self.rowTitleThird];
  189. NSLog(@"選擇結果:%@",text);
  190.  
  191. self.textfield.text=text;
  192. }
  193. //UIPickerViewDataSource
  194.  
  195. //returnsthenumberof'columns'todisplay.
  196. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
  197. {
  198. //設置列數
  199.  
  200. //設置三列:省、市、區
  201. return3;
  202. }
  203.  
  204. //returnsthe#ofrowsineachcomponent..
  205. -(NSInteger)pickerView:(UIPickerView*)pickerViewnumberOfRowsInComponent:(NSInteger)component
  206. {
  207. //設置每列的行數
  208.  
  209. //設置每列的實際行數
  210. NSArray*keyArray=self.sourceDict.allKeys;
  211. if(0==component)
  212. {
  213. returnkeyArray.count;
  214. }
  215. elseif(1==component)
  216. {
  217. NSDictionary*cityDict=self.sourceDict[self.rowTitleFirst];
  218. NSArray*cityArray=cityDict.allKeys;
  219. returncityArray.count;
  220. }
  221. elseif(2==component)
  222. {
  223. NSDictionary*areaDict=self.sourceDict[self.rowTitleFirst];
  224. NSArray*areaArray=areaDict[self.rowTitleSecond];
  225. returnareaArray.count;
  226. }
  227.  
  228. return0;
  229. }
  230. //隱藏鍵盤
  231. -(void)hiddenKeyboard:(UIButton*)button
  232. {
  233. [self.viewendEditing:YES];
  234. }
    \

     





  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved