2013-05-10 50 views
0

我有一個很難理解下面的代碼塊中的cellForRowAtIndexPath:定製電池的再利用5.1

NSString *uniqueIdentifier = @"SliderCellWithComments"; 

SliderCellWithComment *cell = nil; 

cell = (SliderCellWithComment*) [tableView dequeueReusableCellWithIdentifier:uniqueIdentifier]; 

if(!cell) 
{ 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SliderCellWithComment" owner:nil options:nil]; 
    for (id currentObject in topLevelObjects) 
    { 
     if([currentObject isKindOfClass:[SliderCellWithComment class]]) 
     { 
      cell = (SliderCellWithComment*)currentObject; 
      cell.delegationListener = self; //important!! 
      cell.indexPath = [indexPath copy]; //important!! 

      break; 
     } 
    } 

    [cell setNameLabelText:@"Days to display:"]; 
    . 
    . 
    . 

我從StackOverflow上得到這個代碼,直到我試圖運行它,它工作得很好iOS 5.1,它崩潰時發生錯誤:'NSInternalInconsistencyException',原因:'NIB數據無效'。

但我不明白的代碼是,它似乎並沒有真正重用任何東西。

例如: 爲什麼此代碼將值分配給「單元格」兩次?

  1. 細胞=(SliderCellWithComment *)[的tableView dequeueReusableCellWithIdentifier:唯一標識符];

  2. cell =(SliderCellWithComment *)currentObject;

如果2個執行,根據我來說,沒有什麼是被重新使用,因爲小區被分配從新筆尖的值。

我真的不明白使用數組的任一,爲什麼下面的代碼呈現空白單元格:

static NSString *CellIdentifier = @"SliderCellWithComments"; 
SliderCellWithComment *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[SliderCellWithComment alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 

[cell setNameLabelText:@"Days to display:"]; 
cell.delegationListener = self; //important!! 
cell.indexPath = [indexPath copy]; //important!! 
. 
. 
. 
+0

我解決了這個問題,通過將我的單元格中的標籤從歸爲普通的方式更改。但是,我仍然需要了解上面的代碼,請查看我關於數組及其在單元重用中的作用的問題。 – 2013-05-10 08:59:16

回答

0

其實代碼重用細胞。 [tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];要求表格視圖,如果他可以找到一個具有可重用的標識符的單元格,如果有一個單元格可以重複使用,那麼該單元格將不會爲零,並且將不會執行來自if(!cell){}的代碼,如果該表沒有找到一個單元格,那麼將執行if(!cell){}塊,並從xib文件創建一個新的自定義單元格。

I don't really get the use of the Array

您有什麼存在(即數組)是加載從廈門國際銀行文件的自定義視圖的默認方式。

why does the following code render blank cells

因爲在那一塊,你在呼喚從UITableViewCell的一種方法,很可能無法在您的自定義單元格中實現,因爲自定義單元初始化將使用廈門國際銀行文件(即在首先提到的是陣列來實現代碼引用:)