问题
Possible Duplicate:
How to customize the UIDatePicker
I need to customize UIDatePicker as we do support a variety of date formats likedd-MM-yyyy
, dd-MM-yyyy HH:mm:ss
, dd-MM
.
E.g. in "date and time" picker mode, there is no roller for year part in picker.Hence the provided picker modes are not sufficient.
Is there any way to customize a datepicker for the desired formats.
回答1:
Absolutely!
You cannot do this with UIDatePicker
as it only supports 4 date formats, according to the UIDatePicker Class Reference; however you can with a UIPickerView
.
You will need to need to implement UIPickerViewDelegate
methods and provide them with a data source, just like you would with a UITableView
:
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView*)thePickerView {
return /* # of columns you need */;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return /* # of rows per component */;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return /* date source/string/title for the row */;
}
Reference: here
回答2:
A UIDatePicker
supports the four datePickerMode
constants detailed in the documentation. You can't provide custom date formats. However, depending on your application and requirements you could just use a standard picker view: I've done this where I only need to display a list of months.
来源:https://stackoverflow.com/questions/13934477/can-i-customize-uidatepicker