目标C语言 iOS - UIDatePicker改变显示日期动态



我使用的代码从DateCell的例子,我想改变日期在开始日期,我选择(我从某处加载)。所以我在dataArray (kTitleKeykDateKey的字典)和dateCell中改变日期的值是可以的。这里显示了我的日期,但当我点击单元格时,显示了DatePicker里面是StoryBoard的默认日期。我猜问题是在updateDatePicker方法:

- (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总是nil。我试图改变它(在评论中的代码),但它没有帮助(associatedDatepickerCell然后不是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;
}

谁在调用你的updateDatePicker方法?

问题是,在其实现中,您试图获得cellForRowAtIndexPath:单元格,这可能会,也可能不会返回一个单元格,具体取决于时间,滚动位置等:

返回值

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

您应该在返回tableViewDataSource中相应的单元格时更新选择器,这是在您的tableView:cellForRowAtIndexPath:中,它将仅在需要时由表视图惰性调用。

确保你的cellForRowAtIndexPath的代码看起来像:

[tableView dequeueReusableCellWithIdentifier:@"Cell"];

你已经注册类或nib调用tableViews方法(在viewDidLoad为例):

– registerNib:forCellReuseIdentifier:
– registerClass:forCellReuseIdentifier:

所以实际的问题在你的cellForRowForIndexPath:

最新更新