I have the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
{
static NSString *CellID = @"FlourishCustomCell";
FlourishCustomCell *cell = (FlourishCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellID];
if (cell == nil) {
cell = [[[FlourishCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID] autorelease];
cell.frame = CGRectMake(0, 0.0, 292.0, 30);
}
id <NSFetchedResultsSectionInfo> sectionInfo =
[[appDelegate.fetchedResultsController sections] objectAtIndex:indexPath.section];
NSLog(@"DATE:%@", [sectionInfo name]); //Output: March 25
cell.dateLabel.text=[sectionInfo name];
NSLog(@"header cell:%@", cell.dateLabel.text); //Output:header cell:(null)
return cell;
}
else {
static NSString *CellIdentifier = @"IdeaCustomCell";
IdeaCustomCell *cell = (IdeaCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[IdeaCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.frame = CGRectMake(0, 0.0, 292.0, 70);
}
[self configureCell:cell atIndexPath:[self newIndexPathForIndexPath:indexPath]];
return cell;
}
}
The cells display and work fine for the else part (the IdeaCustomCell), but for some extremely frustrating reason, when I set the dateLabel of the FlourishCell, and then immediately try to access that value, I get a null value, even though I just set it! And the cell doesn’t display on screen.
I tried overriding the setter method for dateLabel in the FlourishCustomCell class, and I put an NSLog statement there, but it never gets called for some reason.
I have absolutely no idea what could be causing this. I mean I’m allocating right then and there, but it’s still giving me null. Any ideas?
You need to initialize the label
Solution 1: Initializing cell in code
Solution 2: Initializing cell from nib
EDIT: oops, forgot to return self on init method, updated the answer