Avinash Sharma Avinash Sharma - 1 month ago 8
iOS Question

UITableView custom cell IBOutlets are nil

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?

Answer

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.

Comments