2013-07-11 48 views
0

我有一個UIView包含兩個UITableViews,它們在UIView的導航欄中使用分段控件進行切換。從筆尖加載自定義單元沒有按時完成

第一個表(成分)只使用標準單元格,並且工作正常。

第二個表(食譜)使用從筆尖加載的自定義單元格。問題是,當應用程序啓動和配方表的最後可見的(從狀態保存),當視圖顯示在細胞用標準細胞呈遞。如果用戶循環提到的分段控制,它們在返回到食譜表格後按預期顯示。在視圖控制器的tableView:cellForRowAtIndexPath:

相關部分:

// Check that we are displaying the right table 
if (tableView == self.recipesTable) { 
    static NSString *recipeCellIdentifier = @"RecipeCellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:recipeCellIdentifier]; 

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RecipeCell" owner:self options:nil]; 
    if(nib.count > 0) 
     { 
     cell = self.customCell; 
     } 
     else 
     { 
      NSLog(@"Failed to load CustomCell nib file!"); 
     } 
    } 

    // Set a number of properties of the custom cell 
    // ... 

    return cell; 

self.customCell是一個IBOutlet UITableViewCell中正在使用的文件的所有者(筆尖只包含的UITableViewCell)綁定到實際筆尖文件中的單元格。

對我而言,這表明筆尖不能及時加載,即直到視圖第一次出現之後。

我試圖筆尖裝載移動到viewDidLoad方法,以及迫使reloadDatasetNeedsDisplayviewWillAppear:結束,但也沒有用。

讓我感到困惑的是,只要具有自定義單元格的表格最初不可見,但在啓動後切換到該表格,它就會工作。

回答

0

的問題是不是與筆尖加載,但故事板中撒謊。在UIView中,設置是在任何給定時間只有兩個表格中的一個可見。啓動時,默認情況下使用標準單元的表格可見,但隨後viewWillAppear中的邏輯:確定是否應該隱藏該表格,而是否隱藏另一個(基於保存的狀態)。

原來,當我在啓動時將它們都隱藏並使用保存的狀態取消隱藏其中一個時,所有工作都正常。

0

您是否將UITableViewCell與您的nib文件一起繼承?

因爲你可以嘗試使用:(與RecipeCell作爲子類的名稱)

RecipeCell *cell = (RecipeCell *)[tableView dequeueReusableCellWithIdentifier:recipeCellIdentifier]; 

if (!cell) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RecipeCell" owner:self options:nil]; 

    if(nib.count > 0) 
    { 
     self.customCell = [nib lastObject]; //Assuming you only have one top level object 
            //in the nib 
     cell = self.customCell; 
    } 
    else 
    { 
     NSLog(@"Failed to load CustomCell nib file!"); 
    } 
} 

爲什麼要使用「customCell」作爲真正的屬性?你可以只分配給它這樣的,擺脫了self.customCell

cell = [nib lastObject];