Этот код добавит средство выбора даты в качестве подпредставления UITableView. Этот код предполагает, что мы являемся подклассом UITableViewController.
Мы собираемся оживить сборщик снизу. Наконец, мы подключаем метод scrollViewDidScroll: делегат, чтобы при прокрутке таблицы средство выбора оставалось на месте.
В своем файле .h добавьте свойство для средства выбора.
@property (strong, nonatomic) UIDatePicker *datePicker;
В ваш файл .m мы добавляем средство выбора даты.
- (void)viewDidLoad
{
// Your own customization code.
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 200, 320, 216)];
self.datePicker.hidden = YES;
[self.datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.datePicker];
}
- (void)presentDatePicker
{
if (self.datePicker.hidden == NO) { return; }
// Set the date picker frame to be below the screen.
self.datePicker.frame = CGRectMake(0, self.view.frame.size.height, self.datePicker.size.width, self.datePicker.size.height);
CGRect originFrame = self.datePicker.frame;
// Animate from the bottom.
[UIAnimation animateWithDuration:.3 animations^{
self.datePicker.frame = CGRectMake(0, 200, originFrame.size.width, originFrame.size.height);
}];
}
#pragma mark - Scroll view delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect tableBounds = self.tableView.bounds;
CGRect datePickerFrame = self.datePicker.frame;
self.datePicker.frame = CGRectMake(0, 200 + tableBounds.origin.y, datePickerFrame.size.width, datePickerFrame.size.height);
}
Не забудьте @synthesize ваше свойство и освободить его в viewDidUnload