iOS开发UI篇—UIPickerView控件简单介绍

iOS开发UI篇—UIPickerView控件简单介绍

一.UIPickerView 控件

1.简单介绍:

 2.示例代码

TXViewController.m文件

  1 //  Created by 鑫 on 14-10-15.
  2 
  3 //  Copyright (c) 2014年 梁镋鑫. All rights reserved.
  4 
  5 //
  6 
  7  
  8 
  9 #import "TXViewController.h"
 10 
 11  
 12 
 13 @interface TXViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
 14 
 15 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
 16 
 17 - (IBAction)randomFood;
 18 
 19 @property(nonatomic ,strong)NSArray *foods;
 20 
 21 @property (weak, nonatomic) IBOutlet UILabel *fruitLabel;
 22 
 23 @property (weak, nonatomic) IBOutlet UILabel *mainLabel;
 24 
 25 @property (weak, nonatomic) IBOutlet UILabel *drinkLabel;
 26 
 27  
 28 
 29 @end
 30 
 31  
 32 
 33 @implementation TXViewController
 34 
 35  
 36 
 37 - (void)viewDidLoad
 38 
 39 {
 40 
 41     [super viewDidLoad];
 42 
 43 // Do any additional setup after loading the view,typically from a nib.
 44 
 45     
 46 
 47     //初始化
 48 
 49 //    self.fruitLabel.text =self.foods[0][0];
 50 
 51 //    self.mainLabel.text =self.foods[1][0];
 52 
 53 //    self.drinkLabel.text =self.foods[2][0];
 54 
 55     //手动调用代理方法  所以可用for循环,上面不可以
 56 
 57 //    [self pickerView:nil didSelectRow:0inComponent:0];
 58 
 59 //    [self pickerView:nil didSelectRow:0inComponent:1];
 60 
 61 //    [self pickerView:nil didSelectRow:0inComponent:2];
 62 
 63     for ( intComponent  = 0; Component < self.foods.count; Component++) {
 64 
 65         [self pickerView:nil didSelectRow:0 inComponent:Component];
 66 
 67     }
 68 
 69     
 70 
 71 }
 72 
 73  
 74 
 75  
 76 
 77 //随机选中某一食物
 78 
 79  
 80 
 81 - (IBAction)randomFood {
 82 
 83     
 84 
 85     //生成随机数
 86 
 87     
 88 
 89     //arc4random()%14; 生成0到14的随机数 ,[self.foods[0] count]取出第0列这个数组,这个数组在调用count这个方法
 90 
 91 //    [self.pickerViewselectRow:arc4random()%[self.foods[0] count] inComponent:0 animated:YES];
 92 
 93 //    [self.pickerViewselectRow:arc4random()%[self.foods[1] count] inComponent:1 animated:YES];
 94 
 95 //    [self.pickerViewselectRow:arc4random()%[self.foods[2] count] inComponent:2animated:YES];
 96 
 97     
 98 
 99     //  用for循环
100 
101     for (intcomponent = 0; component < self.foods.count; component++) {
102 
103         //第component列数组的总长度
104 
105         intcount = [self.foods[component]count];
106 
107         //之前的行号
108 
109         intoldrow = [self.pickerView selectedRowInComponent:component];
110 
111         //第几行 默认新的行号跟旧的行号一样
112 
113         introw =oldrow;
114 
115     
116 
117     //    arc4random()%count等价arc4random_uniform(count);
118 
119         //保证行数不一样
120 
121         //之前的行号
122 
123        
124 
125         while (row ==oldrow) {
126 
127             row = arc4random()%count;
128 
129         }
130 
131         //让pickerView主动选中第component列第row行
132 
133         [self.pickerView selectRow:row inComponent:componentanimated:YES];
134 
135         
136 
137         //设置label的文字
138 
139         [self pickerView:nil didSelectRow:row inComponent:component];
140 
141  
142 
143     }
144 
145     
146 
147     
148 
149     
150 
151     
152 
153     
154 
155 }
156 
157  
158 
159 -(NSArray *)foods
160 
161 {
162 
163     if (_foods == nil) {
164 
165         _foods = [NSArray arrayWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"foods" ofType:@"plist"]];
166 
167     }
168 
169     return _foods;
170 
171 }
172 
173  
174 
175  
176 
177  
178 
179 #pragma mark ---数据源方法
180 
181 //一共多少列
182 
183 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
184 
185 {
186 
187     return self.foods.count;
188 
189 }
190 
191 //列。(NSInteger)component  第(NSInteger)componentl列显示多少行
192 
193 -(NSInteger)pickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent:(NSInteger)component
194 
195 {
196 
197    // 先取出第component列的数组
198 
199     NSArray *subfoods = self.foods[component];
200 
201     return subfoods.count;
202 
203 }
204 
205  
206 
207  
208 
209 #pragma mark  - - 代理方法
210 
211 -(NSString *)pickerView:(UIPickerView *)pickerViewtitleForRow:(NSInteger)row forComponent:(NSInteger)component
212 
213 {
214 
215     //先取出第component列,在取出这个数组这一行
216 
217     return self.foods[component][row];
218 
219 }
220 
221 //选中第component列第row行
222 
223 -(void)pickerView:(UIPickerView *)pickerViewdidSelectRow:(NSInteger)row inComponent:(NSInteger)component
224 
225 {
226 
227     
228 
229     NSLog(@"选中了第%d列第%d行",component, row);
230 
231     if (component ==0){
232 
233         self.fruitLabel.text = self.foods[component][row];
234 
235         
236 
237     }
238 
239     else if(component==1)
240 
241     {
242 
243         self.mainLabel.text = self.foods[component][row];
244 
245     }
246 
247     else if(component  == 2)
248 
249     {
250 
251         self.drinkLabel.text = self.foods[component][row];
252 
253     }
254 
255     
256 
257     
258 
259     
260 
261     
262 
263     
264 
265     
266 
267 }
268 
269  
270 
271  
272 
273 @end

实现效果:

二.UIPickerView 用法

1.UIPickerView的常见属性

// 数据源(用来告诉UIPickerView有多少列多少行)

@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;

// 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择)

@property(nonatomic,assign) id<UIPickerViewDelegate>   delegate;

// 是否要显示选中的指示器

@property(nonatomic)        BOOL                      showsSelectionIndicator;

// 一共有多少列

@property(nonatomic,readonly) NSInteger numberOfComponents;

 

2.UIPickerView的常见方法

// 重新刷新所有列

- (void)reloadAllComponents;

// 重新刷新第component列

- (void)reloadComponent:(NSInteger)component;

 

// 主动选中第component列的第row行

- (void)selectRow:(NSInteger)rowinComponent:(NSInteger)component animated:(BOOL)animated;

 

// 获得第component列的当前选中的行号

-(NSInteger)selectedRowInComponent:(NSInteger)component;

 

3.数据源方法(UIPickerViewDataSource)

//  一共有多少列

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

//  第component列一共有多少行

- (NSInteger)pickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent:(NSInteger)component;

 

4.代理方法(UIPickerViewDelegate)

//  第component列的宽度是多少

- (CGFloat)pickerView:(UIPickerView *)pickerViewwidthForComponent:(NSInteger)component;

//  第component列的行高是多少

- (CGFloat)pickerView:(UIPickerView *)pickerViewrowHeightForComponent:(NSInteger)component;

 

//  第component列第row行显示什么文字

- (NSString *)pickerView:(UIPickerView *)pickerViewtitleForRow:(NSInteger)row forComponent:(NSInteger)component;

 

//  第component列第row行显示怎样的view(内容)

- (UIView *)pickerView:(UIPickerView *)pickerViewviewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView*)view;

 

//  选中了pickerView的第component列第row行

- (void)pickerView:(UIPickerView *)pickerViewdidSelectRow:(NSInteger)row inComponent:(NSInteger)component;
原文地址:https://www.cnblogs.com/asd5551680/p/4069707.html