UIDatePicker 在 UITextField 中在 UITableView 实时更新

5
这个问题最近一直困扰着我,希望能得到帮助。
我的目标如下:
1. 为UITableViewCell中的UItextField设置UIDatePickerView。 我已经完成了这些工作,并且可以看到选择器正常工作。每当我更改选择器值时,我使用NSLog来显示当前值,并且对于每次更改都可以正常工作。
这就是我想要的: **每当我更改选择器值时,我需要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
2个回答

9
您不需要实例化多个UIDatePicker。一个就足够了。将其分配给所有文本字段。现在,要更新文本字段,您需要确定正在更新哪个文本字段。为此,您应采用UITextFieldDelegate协议并实现textFieldDidBeginEditing:方法来设置活动文本字段。现在,当在UIDatePicker实例上更改值时,请更新活动文本字段。
但是这还不够。您将面临单元格重用问题。为了解决这个问题,您将不得不维护每一行的日期数组,并在每次在UIDatePicker实例上更改值时更新数组。您可以通过tag标记或获取超级视图(它将是UITableViewCell实例),然后在UITableView上调用indexPathForCell:方法来确定文本字段属于哪个单元格。
另外,return(label.text); [df release];是错误的,因为该方法将在dfrelease之前返回。您应该交换顺序。

大家好,我有一个关于被接受答案的问题。我已经实现了 textFieldDidBeginEditing: 方法来获取当前的 UITextField,但是我该如何从 datePickerValueChanged 方法中更新该字段呢? - AngeloS
我也尝试过这个:- (void)textFieldDidBeginEditing:(UITextField *)textField { textField.text = 从UIDatePicker中获取的全局保存字符串; } - AngeloS
根据错误,我认为 datePicker 没有指向 UIDatePicker 对象。请检查您在 IB 中是否正确设置或者是否在其他地方进行了更改。 - Deepak Danduprolu
1
为什么更改 UITextField 的值会调用 datePickerValueChanged:?如果有一个 setTarget:action.. 的东西,那就把它移除掉。 - Deepak Danduprolu
我把它移除了,然后它就正常工作了。对于这一切我感到很抱歉。我完全忽略了在cellForRowAtIndexPath中设置UITextField时将setTarget:action..设置为datePickerValueChanged的事实... 对于我的疏忽我深表歉意。不过,我会发布一些示例代码,以防有人偶然遇到此问题。 - AngeloS
显示剩余17条评论

1

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

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

cellForRowAtIndexPath中设置标签如下:

textField.tag = indexPath.row;

我假设 dateArray 已经预先填充好,在表格视图第一次被调用时,要么使用实际日期,要么使用空值。如果您尝试替换不存在的内容,显然它将失败。

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

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

第一次可能为空白或预填日期。


1
最好的做法是您应该提出一个新问题,并参考这个问题和答案。 - Kev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接