Avinash Sharma Avinash Sharma - 2 months ago 5
Objective-C Question

UITableView custom cell

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?


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

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 viewController won't be able to load the Cell Nib file, which would result in nil properties of Cell.