簡體   English   中英

UITableView 中 UITextField 中的 UIDatePicker 實時更新

[英]UIDatePicker in UITextField in UITableView realtime update

這個問題最近一直困擾着我很多。 任何幫助,將不勝感激。

這些是我的目標:

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

}

Output 顯示正確。 但我也需要在 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實例上的值時更新該數組。 您可以通過tag文本字段或通過獲取將是UITableViewCell實例的超級視圖然后調用 UITableView 上的UITableView indexPathForCell:方法來識別文本字段屬於哪個單元格。

順便說一句, return (label.text); [df release]; return (label.text); [df release]; 是錯誤的,因為該方法將在dfrelease d 之前返回。 你應該在那里交換訂單。

這只是對已接受解決方案的一系列評論的后續行動。 謝謝大家的幫助。 為了使這項工作,示例代碼如下:

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

    textFieldThatWillBeUpdatedByTheUIDatePicker = (UITextField *)textField;

}

其中textFieldThatWillBeUpdatedByTheUIDatePicker在 header 文件中被聲明為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中設置的位置如下:

textField.tag = indexPath.row;

我假設dateArray是預先填充的,並且在第一次使用真實日期或空白值調用表視圖之前。 如果您嘗試替換不存在的東西,它顯然會失敗。

最后,當在cellForRowAtIndexPath中構建UIDatePicker時,包括這一行來處理單元重用:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM