UITableView中UITextField中的UIDatePicker实时更新

发布于 2024-11-13 00:53:26 字数 2899 浏览 3 评论 0原文

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

这些是我的目标:

  1. 为 UITableViewCell 中的 UItextField 设置 UIDatePickerView。

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

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

这是我的 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;
 }

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

- (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];

输出

显示正确。但我也需要在 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

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.

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.

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)**

This is my CellForRowIndexPath code:

- (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.

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-11-20 00:53:26

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

但遗憾的是这还不够。您将面临单元重复使用的问题。为了解决这个问题,您必须为每行维护一个日期数组,并在每次 UIDatePicker 实例上的值发生更改时更新该数组。您可以通过标记文本字段或获取作为 UITableViewCell 实例的超级视图,然后调用 indexPathForCell:来识别文本字段属于哪个单元格。 UITableView 上的 code> 方法。

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

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 UITextFieldDelegate protocol and implement the textFieldDidBeginEditing: method to set the active text field. Now, when the value is changed on the UIDatePicker instance, update the active text field.

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 UIDatePicker instance. You can identify which cell the text field belongs to by either tagging it or by getting the superview which would be a UITableViewCell instance and then calling indexPathForCell: method on the UITableView.

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

童话 2024-11-20 00:53:26

这只是对已接受解决方案的一系列评论的后续行动。谢谢大家的帮助。为了实现此功能,示例代码如下:

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

    textFieldThatWillBeUpdatedByTheUIDatePicker = (UITextField *)textField;

}

其中 textFieldThatWillBeUpdatedByTheUIDatePicker 在头文件中声明为 UITextField

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

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

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

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

}

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

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

其中 tagcellForRowAtIndexPath< 中设置/code> 像这样:

textField.tag = indexPath.row;

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

最后,当在 cellForRowAtIndexPath 中构建 UIDatePicker 时,请包含此行来处理单元格重用:

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

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

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 textFieldThatWillBeUpdatedByTheUIDatePicker is declared as a UITextField in the header file.

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:

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

Where the tag is set in cellForRowAtIndexPath like this:

textField.tag = indexPath.row;

I'm assuming that the dateArray is 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.

Finally, when the UIDatePicker is being built in cellForRowAtIndexPath include this line to handle the cell reuse:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文