How to show uidatepicker from uitableviewcell

How can I show a UIDatePicker from a UITableViewCell and the datepicker has a toolbar at the top that left you retired? Are there any good tutorials on how to do this?

+6
source share
3 answers

Recently, I just needed to do the same.

  • Insert a UITextField into your UITableViewCell (you may need to create a custom UITableViewCell depending on whether you want it to appear in every dynamic cell of your UITableView or in one static cell).
  • Create properties for UIDatePicker , UIToolbar and UITextField , then in IB connect the UITextField property to your UITextField created in step 1 (if you are using a custom UITableViewCell class that must have a UITextField property):

     @property (strong, nonatomic) UIDatePicker * datePicker; @property (strong, nonatomic) UIToolbar * datePickerToolbar; @property (strong, nonatomic) IBOutlet UITextField *textField; ... @synthesize datePicker, datePickerToolbar, textField; 
  • Set the UIDatePicker , UIToolbar and UITextField :

     - (void)viewDidLoad { // Initialise UIDatePicker datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerModeTime; [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; // method to respond to changes in the picker value // Setup UIToolbar for UIDatePicker datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; [datePickerToolbar setBarStyle:UIBarStyleBlackTranslucent]; UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissPicker:)]; // method to dismiss the picker when the "Done" button is pressed [datePickerToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, doneButton, nil]]; // Note: If you're using dynamic cells, the below 2 lines need to be in your cellForRowAtIndexPath method instead. // Set UITextfield inputView as UIDatePicker textField.inputView = datePicker; // Set UITextfield inputAccessoryView as UIToolbar textField.inputAccessoryView = datePickerToolbar; } 
  • Setting up the dismissPicker method:

     -(void)dismissPicker:(id)sender{ [textField resignFirstResponder]; } 
  • Setting the datePickerValueChanged method:

     - (void)datePickerValueChanged:(id)sender{ NSDate *selectedDate = datePicker.date; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"HH:mm"]; [textField setText:[df stringFromDate:selectedDate]]; } 

Note. In my code, I need an interface to display the time, so the date format is set here ( HH:mm ). Because of this, you will also notice that in my UIDatePicker initialization code in viewDidLoad I set its mode to UIDatePickerModeTime . To select a date, you can set it to UIDatePickerModeDate .

+4
source

You can put an invisible UITextField in a UITableViewCell and set the input view of UITextFields to UIDatePicker. Here is the code:

  yourdatePicker = [[UIDatePicker alloc]init]; yourtextField.inputView = yourdatePicker; 
+3
source

I am using ActionSheetPicker [1]. This allows you to display any type of collector from an action sheet partially overlapping the current scene. In addition, you can add buttons (for example, cancel or "today") and a title at the top of the view.

[1]: Original version of Tim Kinel: https://github.com/TimCinel/ActionSheetPicker Improvements to Hari Karam Singh: https://github.com/Club15CC/ActionSheetPicker Fixed erasures in my plug (I think it now applies to rest): https://github.com/booiiing/ActionSheetPicker

0
source

Source: https://habr.com/ru/post/949942/


All Articles