2013-11-21 24 views
3

我有一個動態tableview其中一個單元格被選中,並顯示另一個單元格下面包含UIDatePicker - 就像在日曆應用程序。dequeueReusableCellWithIdentifier需要0.5秒的單元格包含UIDatePicker

這很有效,但當用戶第一次滾動到單元格的初始加載時(當它的高度爲0時),我遇到了問題。

調查爲什麼它很慢我在NSLogs中調用dequeueReusableCellWithIdentifier調用來查看時間。

- (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個UILabels細胞和的DatePicker是含有的UIDatePicker細胞。

UIDatePicker單元需要大約半秒才能完成。第一次滾動後加載速度快得多,所以沒有明顯的滯後 - 除了第一次點擊日期單元格以及其下方的DatePicker單元格將其高度從0更改爲220.

NSLog第一次打開電池:打開首次在的UIDatePicker小區時

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

我注意到日曆應用程序有一個輕微的延遲,但是這似乎並沒有要,只要它發生在我的應用程序。

我正在測試iPhone 4,這可能是一個促成因素。

有沒有人有任何想法,我可能做錯了或可以做到這一點更快?

+1

這是什麼儀器可以。使用時間分析器,看看它花了那麼久。 – rmaddy

回答

4

我已經通過從單元格中刪除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; 
} 
+0

我有類似的問題,但與textview。以類似的方式解決,一切開始加速。 :) – Nemanja