UIDatePicker in UITextField in UITableView realtime update

前端 未结 2 1368
我寻月下人不归
我寻月下人不归 2020-12-16 06:41

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
相关标签:
2条回答
  • 2020-12-16 07:13

    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.

    0 讨论(0)
  • 2020-12-16 07:26

    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.

    0 讨论(0)
提交回复
热议问题