iOS - UIDatePicker更改显示日期动态

时间:2014-03-22 13:37:13

标签: ios objective-c date uidatepicker datecell

我正在使用来自DateCell example的代码,我想在我选择的开始日期更改日期(我从某处加载)。所以我在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总是为零。我试图更改它(使用注释中的代码)但它没有帮助(associatedDatepickerCell然后没有,但是日期仍然没有变化)。我在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;
}

2 个答案:

答案 0 :(得分:1)

谁在调用您的updateDatePicker方法?

问题在于,在其实现中,您尝试使用cellForRowAtIndexPath:来获取可能会或可能不会返回单元格的单元格,具体取决于时间,滚动位置等:

  

返回值

     

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

您应该在tableViewDataSource内的tableView:cellForRowAtIndexPath:内返回相应的单元格时更新选择器,{{1}}只会在需要时通过表格视图进行懒惰调用。

答案 1 :(得分:0)

确保您的cellForRowAtIndexPath代码如下:

[tableView dequeueReusableCellWithIdentifier:@"Cell"];

你已经注册了类或者nib调用tableViews方法(例如在viewDidLoad中):

– registerNib:forCellReuseIdentifier:
– registerClass:forCellReuseIdentifier:

所以你的实际问题是cellForRowForIndexPath: