我有一個很難理解下面的代碼塊中的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數據無效'。
但我不明白的代碼是,它似乎並沒有真正重用任何東西。
例如: 爲什麼此代碼將值分配給「單元格」兩次?
細胞=(SliderCellWithComment *)[的tableView dequeueReusableCellWithIdentifier:唯一標識符];
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!!
.
.
.
我解決了這個問題,通過將我的單元格中的標籤從歸爲普通的方式更改。但是,我仍然需要了解上面的代碼,請查看我關於數組及其在單元重用中的作用的問題。 – 2013-05-10 08:59:16