1 1.numberOfComponents:返回UIPickerView當前的列數
2 NSInteger num = _pickerView.numberOfComponents;
3 NSLog( @"%d", num);
4 2. - (NSInteger)numberOfRowsInComponent:(NSInteger)component; 返回component列中有多少行。
5 NSInteger numInCp = [_pickerView numberOfRowsInComponent:0];
6 NSLog(@"%d",numInCp);
7 3. - (CGSize)rowSizeForComponent:(NSInteger)component; 返回component中一行的尺寸。
8
9 CGSize size = [_pickerView rowSizeForComponent:0];
10 NSLog(@"%@", NSStringFromCGSize(size));
11
12
13 delegate:
14 2.0 設置UIPickerView代理
15 _pickerView.delegate = self;
16 // 設置UIPickView每行顯示的內容
17 2.1 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
18 {
19 return @"showData";
20 }
21
22
23 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
24
25 // 返回一個視圖,用來設置pickerView的每行顯示的內容。
26 -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
27 {
28 UIView *myView=[[UIView alloc]init];
29 myView.backgroundColor = [UIColor redColor];
30 return myView;
31 }
32 效果:
33
34 dataSource:數據源
35 #pragma mark - dataSource method
36 // 設置每列顯示3行
37 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
38 {
39 return 3;
40 }
41 // 設置顯示2列
42 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
43 {
44 return 2;
45 }
46 4. showsSelectionIndicator:是否顯示指示器,默認為NO
47 _pickerView.showsSelectionIndicator = NO;
48
49 注意:設置UIPickerView的行數與列數需要設置數據源,遵守UIPickerViewDataSource,設置UIPickerView的內容需要設置代理,并且遵守代理方法UIPickerViewDelegate。
50
51
52 5.-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
53 當點擊UIPickerView的某一列中某一行的時候,就會調用這個方法。
54 6. 返回第component列每一行的高度
55 - (CGFloat)pickerView:(UIPickerView *)pickerView
56 rowHeightForComponent:(NSInteger)component;
57
58 7.刷新某一列的數據
59 一旦調用了這個方法,就會重新給數據源發送消息計算這列的行數、重新給代理發送消息獲得這列的內容
60 [pickerView reloadComponent:1];
61 8. 刷新所有列的數據
62 - (void)reloadAllComponents;
63 9. 返回選中的是第component列的第幾行。
64 - (NSInteger)selectedRowInComponent:(NSInteger)component;