0

我正在使用DateCell 示例中的代码,我想在开始时更改我选择的日期(我从某处加载)。因此,我正在更改(带有and的dataArray字典)中的日期值,这没关系。显示了我的日期,但是当我单击该单元格时,显示了来自 StoryBoard 的默认日期。我猜问题出在 updateDatePicker 方法中:kTitleKeykDateKeydateCellDatePicker

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

由于某种原因 associatedDatePickerCell 始终为零。我试图更改它(在注释中使用代码)但它没有帮助(相关的DatepickerCell 然后不是 nil 但日期仍然没有改变)。我有正确的日期itemData

编辑:添加代码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;
}
4

2 回答 2

1

谁在调用你的updateDatePicker方法?

问题在于,在其实现中,您尝试cellForRowAtIndexPath:根据时间、滚动位置等获取可能返回或不返回单元格的单元格:

返回值

表示表格单元格的对象,如果单元格不可见或 indexPath 超出范围,则为 nil。

您应该在返回 中的相应单元格时更新选择器tableViewDataSource,即在您的内部tableView:cellForRowAtIndexPath:,只有在需要时才会被表格视图懒惰地调用。

于 2014-04-01T08:22:40.480 回答
0

确保您的cellForRowAtIndexPath代码如下所示:

[tableView dequeueReusableCellWithIdentifier:@"Cell"];

并且您已经注册了调用 tableViews 方法的类或 nib(例如在 viewDidLoad 中):

– registerNib:forCellReuseIdentifier:
– registerClass:forCellReuseIdentifier:

所以你的实际问题cellForRowForIndexPath:

于 2014-03-30T10:52:43.057 回答