iOS開發中的UIPickerView,iosuipickerview

來源:互聯網
上載者:User

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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.