如何从uitableviewcell显示uidatepicker [英] How to show uidatepicker from uitableviewcell

查看:133
本文介绍了如何从uitableviewcell显示uidatepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示来自UITableViewCell的 UIDatePicker ,并且 datepicker 在顶部有一个工具栏你辞职吗?有没有什么好的教程如何做这个?

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?

推荐答案

我最近只是做同样的事情。 / p>

I've recently just had to do the exact same thing.


  1. UITextField 插入您的 UITableViewCell (您可能需要创建自定义 UITableViewCell ,具体取决于您希望它出现在 UITableView c>,或单个静态单元格)。

  2. UIToolbar UITextField ,然后在IB中将 UITextField code>在步骤1中创建的UITextField (如果您使用自定义的 UITableViewCell 类,那么 UITextField 属性需要去):
  1. Insert a UITextField into your UITableViewCell (you may need to create a custom UITableViewCell depending whether you want it to appear on every dynamic cell of your UITableView or on a single static cell).
  2. Create properties for a UIDatePicker, a UIToolbar, and a UITextField, then in IB hook up the UITextField property to your UITextField created in Step 1 (If you're using a custom UITableViewCell class, that's where the UITextField property would need to go):

@property (strong, nonatomic) UIDatePicker * datePicker;
@property (strong, nonatomic) UIToolbar * datePickerToolbar;
@property (strong, nonatomic) IBOutlet UITextField *textField;

...

@synthesize datePicker, datePickerToolbar, textField;


  • 设置 UIDatePicker code> UIToolbar 和 UITextField

  • Setup your 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's inputView as UIDatePicker    
        textField.inputView = datePicker;
        // Set UITextfield's inputAccessoryView as UIToolbar
        textField.inputAccessoryView = datePickerToolbar;
    }
    


  • 设置 dismissPicker method:

    -(void)dismissPicker:(id)sender{
        [textField resignFirstResponder];
    }
    


  • 设置 datePickerValueChanged method:

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


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

    Note: In my code, I needed the interface to display the time, hence the date format set here (HH:mm). Because of this, you will also notice that in my UIDatePicker initialization code in viewDidLoad, I've set its mode to UIDatePickerModeTime. For date selections, you may want to set it to UIDatePickerModeDate.

    这篇关于如何从uitableviewcell显示uidatepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆