gpt4 book ai didi

iOS - UIDatePicker 更改显示日期动态

转载 作者:行者123 更新时间:2023-11-29 03:07:43 24 4
gpt4 key购买 nike

我正在使用 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 不是零,但日期仍然没有改变)。我在 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: 获取单元格,这可能会或可能不会返回单元格,具体取决于时间、滚动位置等:

Return Value

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

您应该在 tableViewDataSource 中返回相应的单元格时更新选择器,该单元格位于您的 tableView:cellForRowAtIndexPath: 中,它只会被 TableView 延迟调用需要时。

关于iOS - UIDatePicker 更改显示日期动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22578315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com