有没有办法去除dateselect器的“年份”?

我有一个dateselect器,但我只想要显示date和月份,而不是年份,有没有什么办法可以拿出年?

正如其他人所指出的那样:你们被孤立一人,去创造自己的select者,但实际上这并不难。 一个快速的原型,请注意,我使用第一个组件几天,第二年,这应该根据现实世界的应用程序的语言环境:

-(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上find一个示例代码


你可以在GitHub上find的版本知道最喜欢的语言的月份名称

在这里输入图像说明

据我所知,UIDatePicker不能做到这一点。 您可以使用UIPickerView来做到这一点。

为了扩大奥斯卡的答案,你将不得不使用UIPickerView。

但是,这并不意味着你的代码365天,你只需要:

  • 一个组件(车轮)的12个项目(月)
  • 一个组件受损31个项目(天)

select某个项目时,您将收到其委托对象的索引。 在这里,你只需要确定哪个项目是该索引。

本月可以通过键/值风格机制来实现; 或者简单地通过一个数组匹配元件/轮子的数组。 记住,无论如何你都需要一个轮子的数据源,你可以简单地在数据源中查找它; 最有可能是一个数组。

至于那一天,你会得到该项目的索引。 由于您的项目(天)将从1开始,您可以简单地从索引值中取1,以查找用户select的实际date。 (带走1来补偿数组索引)

selectedRowInComponent是你应该看看更好的理解的方法!

然而,将来当你担心你将不得不创build大量的同一个对象时,我build议你看一下Factory Pattern,并且有一些迭代的方式来调用你的Factory方法。 这不是直接关系到你的问题,但它肯定与你的评论有关!

希望这可以帮助!

我在GitHub上find这个项目: PMEDatePicker

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 }