2013-05-10 55 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的一种方法,很可能无法在您的自定义单元格中实现,因为自定义单元初始化将使用厦门国际银行文件(即在首先提到的是阵列来实现代码引用:)