0

I'm testing UITableView with a custom TableViewCell. The custom cell is designed in a .xib file and has its own class called cell, which is a subclass of UITableViewCell:

Cell.m

#import "Cell.h"

@implementation Cell

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    return self;
}

@end

The ViewController class has the UITableView called table in it. It is also Delegate and Datasource for it. Both is set in the Storyboard. Its code looks like this:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.table registerClass:[Cell class] forCellReuseIdentifier:@"cell" ];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [self.table dequeueReusableCellWithIdentifier:@"cell"];
    return cell;
}

@end

If I run the app, I can see the table and it has the right number of cells. But the custom cell is not shown. Each cell of the table is just white.

I read a couple of post on issues like this but no one helped me. Different Tutorials say it should work like this but it doesn't. I think there may is a stupid mistake in it. Thanks for your help.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Björn
  • 608
  • 2
  • 7
  • 19
  • If you don't create the cell on the storyboard, simply dequeuing via the reuse identifier isn't enough. You have to load the cell from its xib. – nhgrif Jun 14 '14 at 13:36
  • But this is just a test. Later in a bigger Project I have to load it via its class because i need the methods in the class. @nhgrif – Björn Jun 14 '14 at 13:53
  • The code for loading the cell via is xib can be contained within the class. Somewhere there has to exist the code to open the xib file and load the visual representation of the cell unless you put it on the storyboard (which is already opened and loaded) or build the cell completely in code. – nhgrif Jun 14 '14 at 13:57
  • ok. I think I almost get what you mean. Can you give me an example code of how it would like? @nhgrif – Björn Jun 14 '14 at 13:59

2 Answers2

0

I'm guessing You forget to set your custom class name in the *.xib file. Set Custom Class property to "cell" in *.xib properties. Please check that property.

David V
  • 2,134
  • 1
  • 16
  • 22
  • In my current project I also used *.xib file to create custom cell. JobInfoCell *cell = (JobInfoCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath]; This code worked for me. JobInfoCell is my custom class for cell. simpleTableIdentifier in you case is @"cell" string. – David V Jun 14 '14 at 14:14
0

You said you're using xib files to instantiate your cells, but you're registering a class instead of xib with this line

[self.table registerClass:[Cell class] forCellReuseIdentifier:@"cell" ];

try using registerNib:forCellReuseIdentifier: instead

Eugene
  • 10,006
  • 4
  • 37
  • 55
  • Thank you for your answer. But this is just a test project, in my big project I need to instantiate it by a class, because I need the methods of it. For example to set a specific text. As far as I understand, by registering a nib you won't be able to call any method of the class the UITableViewCell is from – Björn Jun 14 '14 at 14:09
  • You will be able to call the methods, dequeue the cell as your custom subclass instead of `UITableViewCell`. Like this: `MyCell* cell = [self.table dequeueReusableCellWithIdentifier:@"cell"]; cell.setSomeData = dataSource[indexPath.row]` – Eugene Jun 14 '14 at 14:50
  • 1
    @Burn, what you say about registering the nib is not true. If the cell's UI was setup in a xib, then you need to register nib. As long as you set your cell to be your subclass, then you will have access to that class's methods and properties. You should only register the class if you made the cell completely in code using no xib or storyboard for its UI. This is the correct answer to your problem. – rdelmar Jun 14 '14 at 15:34