7

How can I show a UIDatePicker from UITableViewCell, and have the datepicker have a toolbar at the top that lefts you resign it? Are there any good tutorials on how to do this?

4

3 回答 3

4

我最近不得不做同样的事情。

  1. 将 aUITextField插入您的UITableViewCell(您可能需要创建一个自定义,UITableViewCell具体取决于您希望它出现在您的每个动态单元格上UITableView还是单个静态单元格上)。
  2. UIDatePicker为 a 、 aUIToolbar和 a创建属性UITextField,然后在 IB 中将UITextField属性连接到您UITextField在第 1 步中创建的属性(如果您使用的是自定义UITableViewCell类,那么这就是UITextField属性需要去的地方):

    @property (strong, nonatomic) UIDatePicker * datePicker;
    @property (strong, nonatomic) UIToolbar * datePickerToolbar;
    @property (strong, nonatomic) IBOutlet UITextField *textField;
    
    ...
    
    @synthesize datePicker, datePickerToolbar, textField;
    
  3. 设置您的UIDatePicker,UIToolbarUITextField:

    - (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's inputView as UIDatePicker    
        textField.inputView = datePicker;
        // Set UITextfield's inputAccessoryView as UIToolbar
        textField.inputAccessoryView = datePickerToolbar;
    }
    
  4. 设置dismissPicker方法:

    -(void)dismissPicker:(id)sender{
        [textField resignFirstResponder];
    }
    
  5. 设置datePickerValueChanged方法:

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

注意:在我的代码中,我需要界面来显示时间,因此这里设置了日期格式 ( HH:mm)。因此,您还会注意到在我的UIDatePicker初始化代码中viewDidLoad,我已将其模式设置为UIDatePickerModeTime. 对于日期选择,您可能希望将其设置为UIDatePickerModeDate

于 2013-07-21T20:02:30.403 回答
3

您可以在 UITableViewCell 中放置一个不可见的 UITextField,并将 UITextFields 输入视图设置为 UIDatePicker。这里我们的代码:

 yourdatePicker = [[UIDatePicker alloc]init];
 yourtextField.inputView = yourdatePicker;
于 2013-07-21T19:29:51.227 回答
0

我正在使用 ActionSheetPicker[1]。它允许您从与当前场景部分重叠的操作表中显示任何类型的选择器。此外,您可以在视图顶部添加按钮(如取消或“今天”)和标题。

[1]:Tim Cinel 的原始版本:https ://github.com/TimCinel/ActionSheetPicker Hari Karam Singh 的改进:https ://github.com/Club15CC/ActionSheetPicker 在我的 fork 中修复了弃用(我认为它传播到其他人现在):https ://github.com/booiiing/ActionSheetPicker

于 2013-07-21T19:38:28.237 回答