我在视图中有一个日期选择器,但我只希望它显示日期和月份,而不是年份,有没有办法把这一年带出去?
答案 0 :(得分:11)
正如其他人所指出的:你是独自一人,创建自己的选择器,但实际上这并不困难。一个快速的原型,注意我使用第一个组件几天,第二个多年,这应该根据现实世界的应用程序中的区域设置:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:pickerView numberOfRowsInComponent:(NSInteger)component
{
NSUInteger number = 0;
if (component == 1) {
number = 12;
} else {
NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSUInteger month = [pickerView selectedRowInComponent:1]+1 ;
NSUInteger actualYear = [comps year];
NSDateComponents *selectMothComps = [[NSDateComponents alloc] init];
selectMothComps.year = actualYear;
selectMothComps.month = month;
selectMothComps.day = 1;
NSDateComponents *nextMothComps = [[NSDateComponents alloc] init];
nextMothComps.year = actualYear;
nextMothComps.month = month+1;
nextMothComps.day = 1;
NSDate *thisMonthDate = [[NSCalendar currentCalendar] dateFromComponents:selectMothComps];
NSDate *nextMonthDate = [[NSCalendar currentCalendar] dateFromComponents:nextMothComps];
NSDateComponents *differnce = [[NSCalendar currentCalendar] components:NSDayCalendarUnit
fromDate:thisMonthDate
toDate:nextMonthDate
options:0];
number = [differnce day];
}
return number;
}
-(void)pickerView:pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 1) {
[pickerView reloadComponent:0];
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:@"%d", row+1];
}
您可以在GitHub
找到示例代码你在GitHub上找到的版本知道这个月最喜欢的语言的名字
答案 1 :(得分:2)
Swift 3.0版本:
@IBOutlet weak var pickerCell: UIPickerView!
var months: Array<String> = Calendar.current.monthSymbols
func config() {
pickerCell.delegate = self
pickerCell.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var number = 0
if component == 1 {
return 12
} else if pickerView.numberOfComponents > 1 {
let comps = Calendar.current.dateComponents([.day,.month, .year], from: Date())
let month: Int = pickerView.selectedRow(inComponent: 1)+1
let year: Int = comps.year!
var selectMothComps = DateComponents()
selectMothComps.year = year
selectMothComps.month = month
selectMothComps.day = 1
var nextMothComps = DateComponents()
nextMothComps.year = year
nextMothComps.month = month+1
nextMothComps.day = 1
let thisMonthDate = Calendar.current.date(from: selectMothComps)
let nextMonthDate = Calendar.current.date(from: nextMothComps)
var difference = Calendar.current.dateComponents([.day], from: thisMonthDate!, to: nextMonthDate!)
number = difference.day!
}
return number
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 1 {
pickerView.reloadComponent(0)
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0{
return String(format: "%d", row+1)
} else {
return months[row]
}
}
//centers the views. width > than width of smallest component
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
if component == 0 {
return 45
}
return 140
}
答案 2 :(得分:1)
据我所知,UIDatePicker无法做到这一点。您可以使用UIPickerView来执行此操作。
答案 3 :(得分:1)
为了扩展奥斯卡的答案,你确实必须使用UIPickerView。
但是,这并不意味着你编码365天,你只需要:
选择某个项目后,您将收到其委托对象的索引。在这里,您只需要确定该索引是什么项目。
这个月可以使用键/值样式机制来实现;或者只是一个数值与组件/轮子相匹配的数组。记住,无论如何你需要一个轮子的数据源,你只需在该数据源中查找它;很可能是阵列。
至于当天,您将获得该项目的索引。当您的项目(天)从1开始时,您只需从索引值中取1即可找到用户选择的实际日期。 (拿走1来补偿数组索引)
selectedRowInComponent是您应该查看的方法,以便更好地理解!
但是,将来当您担心必须创建大量相同的对象时,我建议您查看工厂模式,并使用某种形式的迭代来调用Factory方法。这与您的问题没有直接关系,但它肯定与您上面的评论有关!
希望这有帮助!
答案 4 :(得分:1)
我在GitHub上找到了这个项目:PMEDatePicker