0

I am using code from DateCell example and I want to change date at start to date which I choose (I load from somewhere). So I am changing value of date in dataArray (dictionary with kTitleKey and kDateKey) and in dateCell it's okay. There is shown my date but when I clicked on that cell DatePicker is shown and in there is default date from StoryBoard. I guess the problem is in updateDatePicker method:

- (void)updateDatePicker
{
    if (self.datePickerIndexPath != nil)
    {
        UITableViewCell *associatedDatePickerCell = [datePickersTableView cellForRowAtIndexPath:self.datePickerIndexPath];
        /*
        if (associatedDatePickerCell == nil) {
            associatedDatePickerCell = [datePickersTableView dequeueReusableCellWithIdentifier:kDatePickerID];
        }*/
        UIDatePicker *targetedDatePicker = (UIDatePicker *)[associatedDatePickerCell viewWithTag:kDatePickerTag];
        if (targetedDatePicker != nil)
        {
            // we found a UIDatePicker in this cell, so update it's date value
            //
            NSDictionary *itemData = self.dataArray[self.datePickerIndexPath.row - 1];
            [targetedDatePicker setDate:[itemData valueForKey:kDateKey] animated:NO];
        }
    }
}

For some reason associatedDatePickerCell is always nil. I tried to change it (with code in comments) but it didn't help (associatedDatepickerCell then wasn't nil but date still wasn't changing). I have the right date in itemData.

Edit: added code for cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    NSString *cellID;

    if ([self indexPathHasPicker:indexPath])
    {
        // the indexPath is the one containing the inline date picker
        cellID = kDatePickerID;     // the current/opened date picker cell
    }
    else
    {
        // the indexPath is one that contains the date information
        cellID = kDateCellID;       // the start/end date cells
    }
    //if ([self indexPathHasDate:indexPath])
    cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    // if we have a date picker open whose cell is above the cell we want to update,
    // then we have one more cell than the model allows
    //
    NSInteger modelRow = indexPath.row;
    if (self.datePickerIndexPath != nil && self.datePickerIndexPath.row < indexPath.row)
    {
        modelRow--;
    }

    // proceed to configure our cell
    if ([cellID isEqualToString:kDateCellID])
    {
        NSDictionary *itemData = self.dataArray[modelRow];

        // we have either start or end date cells, populate their date field
        //
        cell.textLabel.text = [itemData valueForKey:kTitleKey];
        UILabel *custLabel = (UILabel*)[cell viewWithTag:2];
        custLabel.text = [self.dateFormatter stringFromDate:[itemData valueForKey:kDateKey]];
        UIView *dateView = (UIView*)[cell viewWithTag:3];
        [dateView.layer setBorderWidth:1];
        [dateView.layer setBorderColor:[[UIColor whiteColor] CGColor]];
        cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[itemData valueForKey:kDateKey]];
    }

    [cell setBackgroundColor:[UIColor clearColor]];

    return cell;
}
Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182
  • If associatedDatePickerCell is always nil, then the problem is not in the code you posted. You should log both self.datePickerIndexPath and datePickersTableView to see what they return. Either one of these is nil, or you're trying to access a cell that's not currently being displayed in the table view. – rdelmar Mar 22 '14 at 15:13
  • datePickerIndexPath and datePickersTableView is okay. I guess the problem is that cell is not currently displayed but is going to. The problem is I want to set it before is't displayed. – Libor Zapletal Mar 22 '14 at 15:34
  • Then you need to change itemData without referring to the cell. I don't know why the sample code you're working from does it the way it does -- if you have the indexPath, then you should change that item in the array directly without referring to the cell. – rdelmar Mar 22 '14 at 15:40
  • I am changing it. In viewDidLoad I call method which fill all fields and which set itemData with right Date. I got right date there but when datePicker is shown there is still date from storyboard. – Libor Zapletal Mar 22 '14 at 15:52

2 Answers2

1

Who is calling your updateDatePicker method?

The problem is that in its implementation you try to get the cell with cellForRowAtIndexPath: which may, or may not return a cell depending on the timing, the scrolling position, etc:

Return Value

An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.

You should update the picker when returning the corresponding cell in the tableViewDataSource, that is inside your tableView:cellForRowAtIndexPath:, which will be called lazily by the table view only when needed.

Rivera
  • 10,792
  • 3
  • 58
  • 102
  • It was in DateCell example I mention above. You've been right. When I move code from `updateDatePicker` method to `tableView:cellForRowAtIndexPath:` I get what I want. Thanks – Libor Zapletal Apr 01 '14 at 08:40
0

Make sure that your's cellForRowAtIndexPath has code that looks like:

[tableView dequeueReusableCellWithIdentifier:@"Cell"];

And you have registered class or nib calling tableViews methods (in viewDidLoad for example):

– registerNib:forCellReuseIdentifier:
– registerClass:forCellReuseIdentifier:

So the actual problem in yours cellForRowForIndexPath:.

Bohdan Orlov
  • 274
  • 3
  • 3
  • I don't think there is a problem in 'cellForRowForIndexPath'. In that method I just set right information to DateCell not DatePickerCell. – Libor Zapletal Mar 31 '14 at 09:57
  • But you said that `For some reason associatedDatePickerCell is always nil.` `cellForRowAtIndexPath` always has to return cell, no matter what. – Bohdan Orlov Mar 31 '14 at 11:55
  • I added code which I haven in `cellForRowAtIndexPath`. I added break point to line with `cell = [tableView dequeueReusableCellWithIdentifier:cellID];` and I get cell for `kDatePickerID`. The weird thing is that I get to this break point after calling `[datePickersTableView cellForRowAtIndexPath:self.datePickerIndexPath];`. So first I get nil in `associatedDatePickerCell` and after few Step over I stop at break point in `cellForRowAtIndexPath`. – Libor Zapletal Mar 31 '14 at 12:24