Home > Net >  Swift 5: Add UITableView with dynamic prototype cells in XIB
Swift 5: Add UITableView with dynamic prototype cells in XIB

Time:07-05

I am trying to add a UITableView element to a XIB which I want to present as a sheet from the bottom but I only get the option of static cells. Isn't it possible to have dynamic tables within a XIB or am I missing something? enter image description here

Usually Content Field is visible and I select 1 prototype cell for simple use cases

enter image description here

CodePudding user response:

A prototype cell is a feature of a table view controller, not a table view. You would need to have a table view controller object in your XIB in order to have a prototype cell. Having a table view controller in a XIB is a perfectly legal thing to do, though it is rarely done nowadays, because storyboards are generally easier to work with and are more powerful.

I don't recommend putting a table view controller in a XIB just in order to get a prototype cell. Keep in mind that there are also two other ways of having the runtime instantiate a cell for you when you dequeue:

  • Register a XIB file that contains just a cell object.

  • Or, register a cell type that is a UITableViewCell subclass.

Or just use a storyboard if you really want a prototype cell. A storyboard containing nothing but a single table view controller scene is perfectly legal and viable.

  • Related