Custom UITableViewCell using storyboard and subclass

I created a subclass of UITableViewCell , and I called it DCCustomCell . This is the DCCustomCell.h file.

 #import <UIKit/UIKit.h> @interface DCCustomCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (weak, nonatomic) IBOutlet UILabel *title; @property (weak, nonatomic) IBOutlet UILabel *date; @property (weak, nonatomic) IBOutlet UILabel *price; @end 

All addictions are correctly associated with elements in my iPhone.storyboard file. I also selected tableViewCell in iPhone.storyboard , and I set my cell id to "CustomCell" and the class is DCCustomCell .

This is the UITableViewController viewDidLoad method:

 - (void)viewDidLoad { [super viewDidLoad]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:@"CustomCell"]; // Do any additional setup after loading the view, typically from a nib. } 

and this is tableView: cellForRowAtIndexPath: method:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"CustomCell"; DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier]; NSLog(@"%@" ,cell); cell.imageView.image = [UIImage imageNamed:@"info_button.png"]; cell.title.text = @"Hello!"; cell.date.text = @"21/12/2013"; cell.price.text = @"€15.00"; return cell; } 

The problem is that the View image is properly configured, but all the labels are blank. If I change the name of the imageView property, for example, eventImageView, the image will not be set. This is the result:

enter image description here

I can’t understand how I can solve this problem.

EDIT: if I remove

 [self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:@"CustomCell"]; 

from viewDidLoad everything seems to work. why?

+8
ios uitableview subclass storyboard
source share
4 answers

As you noticed, this works when you delete

 [self.tableView registerClass:[DCCustomCell class] forCellReuseIdentifier:@"CustomCell"]; 

You need to do this only if you use dequeueReusableCellWithIdentifier:indexPath: And you have not set a custom class yet in the identity inspector for this cell. If you use registerClass: forCellReuseIdentifier , the cell will be initialized using initWithStyle:reuseIdentifier: instead of initWithCoder: thereby screwing up your connections made by you in the storyboard.

+7
source share

Have you linked controls to IBOutlet? You can try setting DCCustomCell to the UITableviewCell class directly in the Storyboard. And what you need to set the outputs with controls in the cell

+1
source share

Try:

 if (cell == null) { //create and initialize cell } 

after

 DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier]; 
0
source share

First, you check your sockets correctly that all these tags are plugged in, and write this as well:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"CustomCell"; DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DCCustomCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } NSLog(@"%@" ,cell); cell.imageView.image = [UIImage imageNamed:@"info_button.png"]; cell.title.text = @"Hello!"; cell.date.text = @"21/12/2013"; cell.price.text = @"€15.00"; return cell; } 
0
source share

All Articles