How to Create a UITableViewCell Subclass with Storyboard and an external xib
This is something I have been struggling with since the earliest days of iOS and I have finally figured out how to do it easily, and correctly.
1- In a project that is using a story board, go to file -> New -> Objective-C class
Create your new “CustomTableViewCell” Class and check the box that says to create an XIB. This will generate your tableview cell and a layout file
2- Define your outlets in your CustomTableViewCell.h
@property(nonatomic,weak) IBOutlet UITextView * text;
3- Draw your UI. Open the xib file and draw the elements in to your tableview cell. You can draw whatever you want and then connect the outlets from your CustomTableViewCell to your nib. You can even set the CustomTableViewCell to be a delegate of any of the fields.
4. Ensure that you set the reuse identifier for the cell in the NIB— for example set it to “CustomCell”.
5. Register the NIB with your table view in the ViewDidLoad of your tableViewController:
[self.tableView registerNib:[UINib nibWithNibName:@”CustomTableViewController” bundle:nil] forCellReuseIdentifier:@”CustomCell”];
6. You can dequeue your cell normally now in the view controller:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”CustomCell” forIndexPath:indexPath];
7. Now you can even access properties of the cell directly by casting the UITableViewCell to a CustomTableViewCell in your view controller
CustomTableViewCell * customCell = (CustomTableViewCell*)cell;
customCell.textView.text = @”WOW!”;
The days of having to load the nib manually in your view controller are gone!