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