0

I have created a class which is a subclass of UITableViewCell and I have also created an xib file for the same.

In the xib file I am adding a UITableViewCell and then giving it an identifier "ChatCell" and then in my class I am trying to use this custom cell like this:

static NSString *CellIdentifier =  @"ChatCell";
ChatCustomCell *cell = (ChatCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

In the class I have provided an IBOutlet of UIlabel and connected it to one of the UILabel I have created in the xib but when ever the cell gets created the UILabel stays nil.

Why is this happening?

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Avinash Sharma
  • 665
  • 1
  • 7
  • 23

2 Answers2

3

You need to register your Custom Xib file in your ViewController first.

From Apple Docs on registerClass:forCellReuseIdentifier::

call this method or the registerClass:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.

E.g. Write this line in your viewDidLoad, after setting tableView delegate and dataSource:

[yourTableView registerNib:[UINib nibWithNibName:@"ChatCustomCell" bundle:nil] forCellReuseIdentifier:@"ChatCell"];

If you don't, your TableView won't be able to load the Cell Nib file, which would result in nil IBOutlets of Cell.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
3

for your xib cell may be it returns nil for your cell, write this code may be it helps you.

ChatCustomCell *cell = (ChatCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]

if (cell == nil)
{
    NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:self options:nil];
    cell = [nibArray objectAtIndex:0];
}