UIPickerView:
UIPickerView与UITableView类似,得设置数据源与代理,只是UIPickerView的数据源是设置有几列,每列有几行
UIPickerView的数据源方法:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
// 返回有几列
return self.foodsData.count;
}
// 每一列有几行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
NSArray *items = self.foodsData[component];
return items.count;
}
// 每一列的每一行显示的文字
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
// 每一行显示的控件(通常用来自定义pickerView的cellView)
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;(如果有重用的view就会传进来一个view)
// 选中某列的某行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
// 设置pickerView里的每一个view的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 60;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
UIDatePicker:
// 设置pickerView的样式(是个枚举值)
@property (nonatomic) UIDatePickerMode datePickerMode;
typedef NS_ENUM(NSInteger, UIDatePickerMode) {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer,
};
// 设置pickerView的本地化(设置中文显示)
@property (nonatomic, retain) NSLocale *locale;
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
// 获取pickerView的日期
@property (nonatomic, retain) NSDate *date;
NSDate *date = self.datePicker.date;