UITatePield中的UIDatePicker在UITableView实时更新中 [英] UIDatePicker in UITextField in UITableView realtime update

查看:92
本文介绍了UITatePield中的UIDatePicker在UITableView实时更新中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这些是我的目标:


  1. 设置a UIDatePickerView用于UITableViewCell中的UItextField。

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

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.

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

这是我的CellForRowIndexPath代码:

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

}

输出显示正确。但我还需要在UITextField中进行更改。

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


推荐答案

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

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.

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

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.

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

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.

这篇关于UITatePield中的UIDatePicker在UITableView实时更新中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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