UITableView returns nil property values

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 ( IdeaCustomCell ), but for some extremely unpleasant reason, when I set dateLabel from FlourishCell and then immediately try to access this value, I get null , although I just set it! And the camera does not appear on the screen.

I tried to override the setter method for dateLabel in the FlourishCustomCell class, and I put the NSLog statement there, but for some reason it never gets called.

I have no idea what could be causing this. I mean, what I distribute then and there, but still give me zero. Any ideas?

+2
source share
2 answers

You need to initialize the shortcut

Solution 1: Initialize a cell in code

 @interface FlourishCustomCell : UITableViewCell @property (nonatomic, retain) UILable * dateLabel; @end @implementation FlourishCustomCell @synthesize dateLabel = _dateLabel; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,300,50)]; [self.contentView addSubview:_dateLabel] } return self; } @end 

Solution 2: Initialize a cell from nib

 @interface FlourishCustomCell : UITableViewCell @property (nonatomic, retain) UILable * dateLabel; @end @implementation FlourishCustomCell @synthesize dateLabel = _dateLabel; - (id)init { self = [[[[NSBundle mainBundle] loadNibNamed:@"YOUR_NIB_NAME" owner:nil options:nil] lastObject] retain]; return self; } @end 

EDIT : oops, forgot to return self to init method, updated answer

+4
source

I had the same issue when using feathers, this worked for me

 - (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"Cell"; CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { NSString* nibFile = @"NibForCell"; NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed: nibFile owner:nil options: nil]; cell = [topLevelObjects objectAtIndex: 0]; } cell.cellText.text = @"test"; return cell; } 

just make sure you set the reuse id in nib properties

0
source

All Articles