xcode UITableView 中 UITextField 中的 UIDatePicker 实时更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6208118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:10:49  来源:igfitidea点击:

UIDatePicker in UITextField in UITableView realtime update

iphoneobjective-cxcodeuipickerviewuidatepicker

提问by Legolas

This question has been bugging me a LOT lately. Any help would be appreciated.

这个问题最近一直困扰着我。任何帮助,将不胜感激。

These were my objectives:

这些是我的目标:

  1. Setup a UIDatePickerView for a UItextField in a UITableViewCell.
  1. 为 UITableViewCell 中的 UItextField 设置 UIDatePickerView。

I have done all this so far, and I can see the picker working fine. Whenever I change the picker Value, I use NSLog to display the current value and it is working fine for every change.

到目前为止,我已经完成了所有这些,我可以看到选择器工作正常。每当我更改选择器值时,我都会使用 NSLog 来显示当前值,并且每次更改都可以正常工作。

This is what I want: ** Whenever I change the pickervalue, I need the value to also be changed automatically in the UITextField (which is in the UITableViewCell)**

这就是我想要的: ** 每当我更改选择器值时,我都需要在 UITextField(位于 UITableViewCell 中)中自动更改该值**

This is my CellForRowIndexPath code:

这是我的 CellForRowIndexPath 代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
    textField.clearsOnBeginEditing = YES;
    textField.textAlignment = UITextAlignmentRight;

    [cell.contentView addSubview:textField];
     UIDatePicker *datePicker = [[UIDatePicker alloc] init];
        datePicker.datePickerMode = UIDatePickerModeDate;
        [datePicker addTarget:self action:@selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged];
        datePicker.tag = indexPath.row;
        textField.inputView = datePicker;

        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.dateStyle = NSDateFormatterMediumStyle;
        NSString *local;
        local = [self datePickerValueChangedd: datePicker];  
        NSLog(@"%@", local); //DISPLAYS OUTPUT ONCE
        textField.text =local;
        [datePicker reloadInputViews];
        [datePicker release];




// Configure the cell...

NSInteger row = [indexPath row];
cell.textLabel.text = [doseInfoDetailsArray objectAtIndex:row];

return cell;
 }

I am also posting other code below.

我还在下面发布了其他代码。

- (NSString *)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];

NSLog(@"%@", label.text); // DISPLAYS OUTPUT WHENEVER PICKER IS MOVED
doseInfoDetailsTable.reloadData;
return (label.text);
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(t) name:TestNotification object:label];
[df release];

}

}

Output displays correctly. But I need it to change in the UITextField also.

输出显示正确。但我也需要它在 UITextField 中改变。

    2011-06-01 17:32:23.740 Tab[13857:207] Jun 1, 2017
    2011-06-01 17:32:23.740 Tab[13857:207] Jun 1, 2017
    2011-06-01 17:32:25.321 Tab[13857:207] Dec 1, 2017
    2011-06-01 17:44:51.453 Tab[13857:207] Jun 1, 2017
    2011-06-01 17:44:52.605 Tab[13857:207] Jun 1, 2018
    2011-06-01 17:44:53.467 Tab[13857:207] Jun 2, 2018
    2011-06-01 17:44:54.247 Tab[13857:207] Jun 5, 2018

回答by Deepak Danduprolu

You don't need to instantiate multiple instances of UIDatePicker. One should do. Assign it to all the text fields. Now to update the text field, you will have to identify which text field is being updated. For this you should adopt the UITextFieldDelegateprotocol and implement the textFieldDidBeginEditing:method to set the active text field. Now, when the value is changed on the UIDatePickerinstance, update the active text field.

您不需要实例化 的多个实例UIDatePicker。一个应该做。将其分配给所有文本字段。现在要更新文本字段,您必须确定正在更新的文本字段。为此,您应该采用UITextFieldDelegate协议并实现textFieldDidBeginEditing:设置活动文本字段的方法。现在,当UIDatePicker实例上的值更改时,更新活动文本字段。

But this is sadly not enough. You will face a problem with cell reuse. To counter this, you will have to maintain an array of dates for each of your rows and update the array every time the value is changed on the UIDatePickerinstance. You can identify which cell the text field belongs to by either tagging it or by getting the superview which would be a UITableViewCellinstance and then calling indexPathForCell:method on the UITableView.

但遗憾的是,这还不够。您将面临单元重用的问题。为了解决这个问题,您必须为每一行维护一个日期数组,并在每次更改UIDatePicker实例上的值时更新该数组。您可以识别文本字段属于由其中任何一个细胞tag晋,或通过让这将是一个在上海华UITableViewCell实例,然后调用indexPathForCell:的方法UITableView

And on a side note, return (label.text); [df release];is wrong as the method will return prior to dfbeing released. You should swap the order there.

而在一个侧面说明,return (label.text); [df release];是错误的,因为该方法将之前返回dfreleased。你应该在那里交换订单。

回答by AngeloS

This is just a follow up to a flurry of comments on the accepted solution. Thank you all for your help. To make this work, the sample code is as follows:

这只是对已接受解决方案的一系列评论的跟进。谢谢大家的帮助。为了完成这项工作,示例代码如下:

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    textFieldThatWillBeUpdatedByTheUIDatePicker = (UITextField *)textField;

}

Where textFieldThatWillBeUpdatedByTheUIDatePickeris declared as a UITextFieldin the header file.

where在头文件中textFieldThatWillBeUpdatedByTheUIDatePicker被声明为 a UITextField

Finally, here is the method that changes the cell:

最后,这是更改单元格的方法:

- (NSString *)datePickerValueChanged:(UIDatePicker*) datePicker{

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;

    textFieldThatWillBeUpdatedByTheUIDatePicker.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];

}

To handle cell reuse, do what @Deepak said with regards to an array you can use this line:

要处理单元格重用,请按照@Deepak 所说的关于数组的内容进行操作,您可以使用此行:

[dateArray replaceObjectAtIndex:textFieldThatWillBeUpdatedByTheUIDatePicker.tag withObject:[NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]]];

Where the tagis set in cellForRowAtIndexPathlike this:

tag被设置在cellForRowAtIndexPath这样的:

textField.tag = indexPath.row;

I'm assuming that the dateArrayis pre-populated and prior to the table view getting called for the first time either with real dates or blank values. If you try to replace something that isn't there it is going to fail obviously.

我假设dateArray是预先填充的,并且在第一次使用实际日期或空白值调用表视图之前。如果你试图替换不存在的东西,它显然会失败。

Finally, when the UIDatePickeris being built in cellForRowAtIndexPathinclude this line to handle the cell reuse:

最后,在UIDatePicker构建时cellForRowAtIndexPath包括这一行来处理单元重用:

textField.text = [dateArray objectAtIndex:indexPath.row];

The first time around it may be blank or pre-populated with a date.

第一次可能是空白的或预先填充了日期。