0

This is a dynamic prototype UITableView: In my cellForRowAtIndexPath:

  1. I have dequequed my custom cell and this works just fine:

    TimeSetViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"timeCell" forIndexPath:indexPath];
    
  2. Next I set TimePicker to UITextField in timeCell and this works fine as well (i.e. the time picker displayed onclick of UITextField in timeCell) :

    // Start Time
    UIDatePicker *sTimePicker = [[UIDatePicker alloc]init];
    sTimePicker.datePickerMode = UIDatePickerModeTime;
    sTimePicker.backgroundColor = [UIColor whiteColor]
    [cell.startTripTime setInputView:sTimePicker];
    
    // End Time
    UIDatePicker *eTimePicker = [[UIDatePicker alloc]init];
    eTimePicker.datePickerMode = UIDatePickerModeTime;
    eTimePicker.backgroundColor = [UIColor whiteColor];
    [cell.endTripTime setInputView:eTimePicker];
    
  3. Next I wanted to update the cell's UITextField through an event, in which I have understood from @Mani's post, I have set:

    sTimePicker.tag = 1;
    eTimePicker.tag = 2;
    
    [sTimePicker addTarget:self action:@selector(updateTextField:)
         forControlEvents:UIControlEventValueChanged];
    [eTimePicker addTarget:self action:@selector(updateTextField:)
          forControlEvents:UIControlEventValueChanged];
    

    And in my custom method -(void)updateTextField:(UIDatePicker *)sender I have included this:

    //Date Formatter
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setTimeStyle: NSDateFormatterShortStyle];
    
    if(sender.tag == 1){
    
        //Get Start Date
        UIDatePicker *starttimepicker = (UIDatePicker*)sender.inputView;
        thisStartTime = [starttimepicker date];
    
        //Display Date
        NSString *startTimeText = [dateFormat stringFromDate:thisStartTime];
        sTimeText = [NSString stringWithFormat:@"%@",startTimeText];
    
    }
    if(sender.tag == 2){
        //Get End Date
        UIDatePicker *endtimepicker = (UIDatePicker*)sender.inputView;
        thisEndTime = [endtimepicker date];
    
        //Display Date
        NSString *endTimeText = [dateFormat stringFromDate:thisEndTime];
        eTimeText = [NSString stringWithFormat:@"%@",endTimeText];
    }
    

Question Update

  1. I managed to pass startTimeText and endTimeText to cell.startTripTime.text and cell.endTripTime.text through sTimeText and eTimeText.

  2. And I used [_tableView reloadData]; in -(void)updateTextField:(UIDatePicker *)sender (after the two if blocks) and the data is displayed in my cell.startTripTimeand cell.endTripTime UITextField, but it appears the same in across of my sections, while I want it to change only the first section's time, how can I fix that?

    //I couldn't include the image but the output of the UITableView looks like this:
    
    _________________________
    Section 1
    _________________________
    Start Time : 4:50 PM
    _________________________
    End Time: 5:50 PM
    _________________________
    Section 2
    _________________________
    Start Time : 4:50 PM
    _________________________
    End Time: 5:50 PM
    _________________________
    
  3. And the [_tableView reloadData]; seems to interfere and reloads the view while I haven't even finish picking the time that I wanted, how can I fix this too?

Community
  • 1
  • 1
F.Lee
  • 1
  • 2

1 Answers1

0

The action method should be sent to one of the picker, the startStripTime seems to be a UIView instance not a UIDatePicker instance.
It is he date picker that changes its value, not the view containing it.
You should change also the update method accordingly.

Andrea
  • 26,120
  • 10
  • 85
  • 131
  • Thanks Andrea, I noticed it should have been `-(void)updateTextField:(UIDatePicker *)sender` and I have already changed it in my post. By the way I have set the inputView of `startTripTime`, that makes it a time (date) picker (I got my reference from [here](http://www.codingexplorer.com/replace-keyboard-with-uidatepicker/) ). But next problem is that how am I able to change the text of my `cell.startTripTime` UITextField in respond to that event? – F.Lee May 17 '15 at 07:22
  • I see, you rewrite inputView by setting it to the date piker instance. I thought it was a custom setter to add the pick as a subview to a container view. But now I don't get it the -updateTextField is called – Andrea May 17 '15 at 07:40
  • Now i understood your first comment and changed to this- action sent to a time picker (sTimePicker): `[sTimePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];`, sorry for that. So now: in `-updateTextField` setting the text `sender.text = [NSString stringWithFormat:@"%@",startTimeText];` should be replaced with `cell.startTripTime.text` _but_ `cell` was initialised in `cellForRowAtIndexPath:` not in `-updateTextField`, how can I pass the "startTimeText" to "cell.startTripTime.text" ? – F.Lee May 17 '15 at 07:56
  • Why don't you create the pickers inside the cell? and add the cell as the target for the action of the picker, so you can reference the cell text field from the inside (or we are talking about a cell not related to the picker cell). I don't understand the patter that you are applying and what you want to achieve. Maybe a picture of your UI would help. – Andrea May 17 '15 at 09:30
  • Hmm, let me explain how it looks like since I do not have enough reputations to include an image. [1] In my _UITableView_, I have this custom cell ( _UITableViewCell_ class _TimeSetViewCell_ , identifier _timeCell_) [2] In that cell I have two _UITextField_ for _starttime_ and _endtime_ [3] Each time I click on the _UITextField_ in that cell, it displays a _UIDatePicker_ in which I have set the format to be a time picker (this works) [4] When I select a time from the picker the _UITextField_ should update itself with the time I have selected in text. – F.Lee May 17 '15 at 09:46
  • I have used `NSLog` to track the changes from my picker by printing `startTimeText`, and the selected time is logged e.g. 5: 40 PM (this part works too). I just couldn't pass `startTimeText` to my `cell.startTripTime.text` for it to be displayed. Do I need to use `[_tableView reloadData]`? – F.Lee May 17 '15 at 09:50
  • If you save the string result from the date picker in an ivar of the view controller (that i guess is the delegate/datasource of the tvc), then you can pass it to the cell by calling reloadData – Andrea May 17 '15 at 12:57
  • Andrea, be it iVar or global Variable it works just fine, I have tried and thanks it works! Now that I have a more severe question in my **Question Update** above, do you have any idea on how to work on it? – F.Lee May 17 '15 at 13:45
  • @F.Lee please do not update the original question with another question, just create a bran new question. – Andrea May 17 '15 at 16:05
  • Alright noted, will go along the convention next time. For now, I guess I shall further investigate into my own question and hope that I could find a workaround and share a solution. Thanks for all your help and advices all the while Andrea, appreciate it. – F.Lee May 21 '15 at 19:30