这个类添加了 UIDatePicker 或 UIPickerView,当在您的 UITableView 中的单元格上点击时,会展开或折叠。
受 Apple 的 DateCell 示例和 andjash 的 DateCellsController 启发。
iOS 6.0
#import "PickerCells.h"
引入实例化和设置 PickerCellsController
类。
self.pickersController = [[PickerCellsController alloc] init];
[self.pickersController attachToTableView:self.tableView tableViewsPriorDelegate:self withDelegate:self];
使用相应的 indexPath 添加 UIPickerView
和 UIDatePicker
实例
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.dataSource = self;
NSIndexPath *pickerIP = [NSIndexPath indexPathForRow:1 inSection:1];
[self.pickersController addPickerView:pickerView forIndexPath:pickerIP];
UIDatePicker *datePicker1 = [[UIDatePicker alloc] init];
datePicker1.datePickerMode = UIDatePickerModeDate;
datePicker1.date = [NSDate date];
NSIndexPath *path1 = [NSIndexPath indexPathForRow:2 inSection:0];
[self.pickersController addDatePicker:datePicker1 forIndexPath:path1];
这个类不负责为您提供选择器选中项的信息。您应该自行完成。但是,您可以通过使用相应的单元格 indexPath 从 PickerCellsController
对象中获取选择器。
id picker = [self.pickersController pickerForOwnerCellIndexPath:indexPath];
if ([picker isKindOfClass:UIDatePicker.class]) {
UIDatePicker *datePicker = (UIDatePicker *)picker;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
cell.textLabel.text = [dateFormatter stringFromDate:[datePicker date]];
}
pod 'PickerCells'
MIT