iOS開發中的UIPickerView,iosuipickerview
UIPickerViewUIPickerView的常見屬性
// 資料來源(用來告訴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;
UIPickerView的常見方法
// 重新重新整理所有列- (void)reloadAllComponents;// 重新重新整理第component列- (void)reloadComponent:(NSInteger)component;// 主動選中第component列的第row行- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;// 獲得第component列的當前選中的行號- (NSInteger)selectedRowInComponent:(NSInteger)component;
資料來源方法(UIPickerViewDataSource)
// 一共有多少列- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;// 第component列一共有多少行- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
代理方法(UIPickerViewDelegate)
// 第component列的寬度是多少- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;// 第component列的行高是多少- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;// 第component列第row行顯示什麼文字- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;// 第component列第row行顯示怎樣的view(內容)- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;// 選中了pickerView的第component列第row行- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
注意點:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
這個方法的重用 view 在 ios6之後就一直為空白。如果需要適配 ios6之前的系統,為了效能考慮,應該將這個 view 利用起來。
在ios8之前,UIPickerView的高度是(162)固定的,不能被修改。在 ios8後,它的高度就可以修改了。
pickerView 的高度設定注意點:
如果沒有主動設定高度,或者設定為0。則高度預設值216
如果設定的高度大於0且小於180,則高度預設值162
如果設定的高度大於等於180且小於216,怎麼高度預設值為180
pickerView 的寬度設定:
初始化時沒有給定寬度,或者給定的寬度為0,則寬度預設等於螢幕的寬度。
給定了一個不為大於0的寬度,則寬度就等於給定的值。
UIDatePicker
UIDatePicker常見屬性
// datePicker的顯示模式@property (nonatomic) UIDatePickerMode datePickerMode;// 顯示的地區語言@property (nonatomic, retain) NSLocale *locale;
監聽UIDatePicker的選擇
- 因為UIDatePicker繼承自UIControl,所以通過addTarget:…監聽
代碼建立UIDatePicker
// 1.建立日期選取器UIDatePicker *datePicker = [[UIDatePicker alloc] init];// 2.設定日期顯示地區datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];// 3.設定日期模式datePicker.datePickerMode = UIDatePickerModeTime;// 4.0 設定最小時間(從目前時間起的前20年)datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:-(365 * 24 * 3600 * 20)];// 4.1 設定最大時間(從目前時間起的後20年)datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:365 * 24 * 3600 * 20];// 5.設定時間間隔。(該間隔要能夠讓60整除)datePicker.minuteInterval = 6;