Iphone UITableView实时更新中UITextField中的UIDatePicker

Iphone UITableView实时更新中UITextField中的UIDatePicker,iphone,objective-c,xcode,uipickerview,uidatepicker,Iphone,Objective C,Xcode,Uipickerview,Uidatepicker,这个问题最近一直困扰着我。任何帮助都将不胜感激 我的目标是: 为UITableViewCell中的UItextField设置UIDatePickerView 到目前为止,我已经完成了所有这些,我可以看到选择器工作得很好。每当我更改选择器值时,我都会使用NSLog显示当前值,并且每次更改都可以正常工作 这就是我想要的: **每当我更改pickervalue时,我需要在UITextField(位于UITableViewCell中)中自动更改该值)** 这是我的CellForRowIndexPath代

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

我的目标是:

  • 为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
    

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

    但遗憾的是,这还不够。您将面临单元重用的问题。要解决这个问题,您必须为每行维护一个日期数组,并在
    UIDatePicker
    实例上每次更改值时更新该数组。您可以通过
    标记它,或者通过获取superview(它将是
    UITableViewCell
    实例,然后调用
    UITableView
    上的
    indexPathForCell:
    方法来识别文本字段所属的单元格


    在旁注上,
    return(label.text);[df释放]
    错误,因为该方法将在
    df
    release
    d之前返回。你应该在那里交换订单

    这只是对公认解决方案的一系列评论的后续。谢谢大家的帮助。为了实现这一点,示例代码如下所示:

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
    
        textFieldThatWillBeUpdatedByTheUIDatePicker = (UITextField *)textField;
    
    }
    
    其中,将由UIDatePicker更新的
    TextField在头文件中声明为
    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]]];
    
    其中,
    标记
    cellforrowatinexpath
    中设置如下:

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

    最后,在
    UIDatePicker
    内置于
    cellforrowatinexpath
    中时,包括此行以处理单元重用:

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

    第一次约会可能是空白的,或者预先填上了日期。

    @Siddharth&@Deepak:你们是StackOverflow上我最喜欢的人。有没有办法在StackOverflow上给你们发送即时消息,并在我发布时提出问题(或)通知你们?请允许我尝试一下@Deepak提供的解决方案,我会竖起大拇指并打勾:)谢谢。哈哈,我很高兴你这么认为,但还有很多其他用户比我知道的更多:)无论哪种方式,我都不认为有任何即时通讯选项,但我一天中大部分时间都是这样,所以不管怎样,我肯定会遇到你的问题。话虽如此,SO上精心设计的问题的回复率非常高,所以你不会长时间没有帮助:)嘿,伙计们。你能帮我回答关于UISearchBars的问题吗?为什么
    UITextField
    调用
    datePickerValueChanged:
    的值发生了变化?如果有一个
    setTarget:action..
    对象,请将其删除。理想情况下,您应该提出一个新问题,并参考此问题和答案。