【问题标题】:dequeueReusableCellWithIdentifier takes 0.5 seconds for cell containing UIDatePicker对于包含 UIDatePicker 的单元格,dequeueReusableCellWithIdentifier 需要 0.5 秒
【发布时间】:2013-12-06 06:27:31
【问题描述】:

我有一个动态表格视图,其中一个单元格被选中,并在其下方显示另一个包含UIDatePicker 的单元格 - 就像在日历应用程序中一样。

这很好用,但是当用户第一次滚动到单元格时(当它的高度为 0 时),我在单元格的初始加载时遇到了问题。

调查为什么它很慢,我将 dequeueReusableCellWithIdentifier 调用封装在 NSLogs 中以查看时间。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [[cellIdentifiers objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    NSLog(@"Creating cell:%@", CellIdentifier);
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSLog(@"Finished cell:%@", CellIdentifier);
    return cell;
}

NSLog 显示:

14:47:42.427 Creating cell:Date
14:47:42.437 Finished cell:Date
14:47:42.470 Creating cell:DatePicker
14:47:43.006 Finished cell:DatePicker
14:47:43.046 Creating cell:Time
14:47:43.055 Finished cell:Time
14:47:44.753 Creating cell:DatePicker
14:47:45.253 Finished cell:DatePicker

日期/时间是包含 2 个 UILabel 的单元格,而 DatePicker 是包含一个 UIDatePicker 的单元格。

UIDatePicker 单元大约需要半秒才能完成。第一次滚动后,它的加载速度要快得多,因此没有明显的延迟 - 除了第一次点击日期单元格时,它下方的 DatePicker 单元格将其高度从 0 更改为 220。

首次打开单元格的NSLog:

14:59:34.334 Creating cell:DatePicker
14:59:34.877 Finished cell:DatePicker

我注意到日历应用在第一次打开 UIDatePicker 单元格时有轻微的延迟,但这似乎不像我的应用那样长。

我正在 iPhone 4 上进行测试,这可能是一个促成因素。

有没有人知道我可能做错了什么或可以做些什么来加快速度?

【问题讨论】:

  • 这就是 Instruments 的用途。使用时间分析器,看看它花了这么长时间。

标签: ios uitableview uidatepicker


【解决方案1】:

我已通过从单元格中删除 UIDatePicker、以编程方式创建 UIDatePicker 并将其作为子视图添加到 cellForRowAtIndexPath 中的单元格 contentView 来解决此问题。

我在 viewDidLoad 中初始化了一个 datePicker 和 timePicker。

- (void)viewDidLoad
{
    [super viewDidLoad];
    datePicker = [[UIDatePicker alloc] init];
    [datePicker setDatePickerMode:UIDatePickerModeDate];
    timePicker = [[UIDatePicker alloc] init];
    [timePicker setDatePickerMode:UIDatePickerModeTime];
}

然后在 cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    NSString *CellIdentifier = [[cellIdentifiers objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if ([CellIdentifier isEqualToString:@"DatePicker"] && datePickerOpen)
    {
        [cell.contentView addSubview:datePicker];
    }
    else if ([CellIdentifier isEqualToString:@"TimePicker"] && timePickerOpen)
    {
        [cell.contentView addSubview:timePicker];
    }

    return cell;
}

【讨论】:

  • 我遇到了类似的问题,但使用的是 textview。以类似的方式解决,一切都开始加速。 :)
猜你喜欢
  • 2013-01-05
  • 2016-06-07
  • 2017-05-14
  • 2011-12-10
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多