I'm having a hard time understanding the following block of code inside cellForRowAtIndexPath:
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:"];
.
.
.
I got this code from StackOverflow and it worked fine until I tried running it on iOS 5.1, where it crashes with an error: 'NSInternalInconsistencyException', reason: 'The NIB data is invalid.'
But what I do not understand about the code is that it doesn't seem to really re-use anything.
For instance: Why does this code assign a value to "cell" twice?
cell=(SliderCellWithComment*)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
cell = (SliderCellWithComment*)currentObject;
If 2 executes, according to me, nothing is being re-used since the cell is assigned a value from new nib.
I don't really get the use of the Array either, why does the following code render blank cells:
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!!
.
.
.