Ios – How to use static cells in UITableView without using Storyboards

iosuitableview

I have an existing ViewController + xib in my project, and now I want to add a UITableView with static cells, like this:

TableView with static cells

But when I drag a UITableView onto my screen I don't have the "Content > Static" menu in the Attributes Inspector.

I've tried making my controller subclass UITableViewController, but that doesn't help — I still don't get the option to use static cells in Attributes Inspector.

I've looked around on StackOverflow but haven't found any existing answers to this question. All the existing questions relate to Storyboards (which I'm not using) instead of xib files.

My guess is that Xcode does some kind of magic when you add a UITableViewController to a storyboard, but not when you change an existing xib to inherit from UITableViewController.

Any advice how how to add a table view with static cells to an existing xib?

Best Answer

Static table view cells are only available when using storyboards. However, if you aren't using storyboards for your entire UI you can still use them for individual screens instead of a collection of screens.

To do this you can create a UIStoryboard file with a single view controller on it that has it's File's Owner set to your custom view controller subclass. Set the VC's identifier to some value. When you want to display this, get the storyboard and then instantiate your view controller subclass by creating the VC from your storyboard.

UIStoryboard *tableViewStoryboard = [UIStoryboard storyboardWithName:@"your storyboard" bundle:nil];
CustomViewController = [tableViewStoryboard instantiateViewControllerWithIdentifier:@"custom identifier"];

You can present this VC as usual in your app.

This is a great way to start using storyboards without having to convert your entire app to use them.