Picker View和Date Picker是iOS开发中很常用的两个空间,设置出生日期,选择国家城市省份以及性别啊等等,都会用都这两个控件。
一、Picker View:
@interface UIPickerView : UIView <NSCoding, UITableViewDataSource> @property(nullable,nonatomic,weak) id<UIPickerViewDataSource> dataSource; // default is nil. weak reference @property(nullable,nonatomic,weak) id<UIPickerViewDelegate> delegate; // default is nil. weak reference @property(nonatomic) BOOL showsSelectionIndicator; // default is NO // info that was fetched and cached from the data source and delegate @property(nonatomic,readonly) NSInteger numberOfComponents; - (NSInteger)numberOfRowsInComponent:(NSInteger)component; - (CGSize)rowSizeForComponent:(NSInteger)component; // returns the view provided by the delegate via pickerView:viewForRow:forComponent:reusingView: // or nil if the row/component is not visible or the delegate does not implement // pickerView:viewForRow:forComponent:reusingView: - (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component; // Reloading whole view or single component - (void)reloadAllComponents; - (void)reloadComponent:(NSInteger)component; // selection. in this case, it means showing the appropriate row in the middle - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; // scrolls the specified row to center. - (NSInteger)selectedRowInComponent:(NSInteger)component; // returns selected row. -1 if nothing selected @end
可以看到,它有一个数据源和一个代理属性,分别遵守UIPickerViewDataSource和UIPickerViewDelegate协议。
@protocol UIPickerViewDataSource<NSObject> @required // returns the number of 'columns' to display. //返回一共有多少列 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; // returns the # of rows in each component.. //返回第component列有多少行 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; @end @protocol UIPickerViewDelegate<NSObject> @optional // returns width of column and height of row for each component. //返回第component列的宽度 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component; //返回第component列的行高 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component; // these methods return either a plain NSString, a NSAttributedString, or a view (e.g UILabel) to display the row for the component. // for the view versions, we cache any hidden and thus unused views and pass them back for reuse. // If you return back a different object, the old one will be released. the view will be centered in the row rect //返回每一行的标题 - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; //返回每一行的富文本标题 - (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0); // attributed title is favored if both methods are implemented - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view; //主动选中第component列第row行时调用该方法,一定是手动选择时才会调用 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; @end
// // ViewController.m // // Created by Daniel on 16/3/21. // Copyright © 2016年 Daniel. All rights reserved. // #import "ViewController.h" @interface ViewController () <UIPickerViewDelegate, UIPickerViewDataSource> @property (weak, nonatomic) IBOutlet UIPickerView *pickerView; @property (weak, nonatomic) IBOutlet UILabel *frultLable; @property (weak, nonatomic) IBOutlet UILabel *mainLable; @property (weak, nonatomic) IBOutlet UILabel *drinkLable; @property (nonatomic, strong) NSArray *foods; @end @implementation ViewController - (NSArray *)foods { if (_foods == nil) { NSString *path = [[NSBundle mainBundle]pathForResource:@"foods.plist" ofType:nil]; _foods = [NSArray arrayWithContentsOfFile:path]; } return _foods; } /** * 点击时随机给lable赋值 * * @param sender */ - (IBAction)random:(id)sender { for (int i = 0; i < 3; i++) { NSInteger count = [self.foods[i] count]; int random = arc4random_uniform((u_int32_t)count); //pickerView每一列随机显示一行 [self.pickerView selectRow:random inComponent:i animated:YES]; //将选中的值显示到lable上 [self pickerView:_pickerView didSelectRow:random inComponent:i]; } } - (void)viewDidLoad { [super viewDidLoad]; //初始化lable for (int i = 0; i < 3; i++) { [self pickerView:_pickerView didSelectRow:0 inComponent:i]; } } #pragma mark - <UIPickerViewDataSource> // returns the number of 'columns' to display. /** * 返回一共有多少列 * * @param pickerView * * @return */ - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return self.foods.count; } /** * 返回第component列有多少行 * * @param pickerView * @param component * * @return */ - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [self.foods[component] count]; } #pragma mark - <UIPickerViewDelegate> /** * 返回每一行的高度 * * @param pickerView * @param component * * @return */ - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 40; } /** * 返回每一行的标题 * * @param pickerView * @param row * @param component * * @return */ - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return self.foods[component][row]; } /** * 主动选中第component列第row行时调用该方法 * * @param pickerView * @param row * @param component */ - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { switch (component) { case 0: _frultLable.text = self.foods[component][row]; break; case 1: _mainLable.text = self.foods[component][row]; break; case 2: _drinkLable.text = self.foods[component][row];; break; } } @end
二、Date Picker: