1

I want to use static cells in a UITableView in my storyboard. But also I want those cells to be loaded from a custom .xib file with a custom UITableViewCell subclass.

I want to do this because I want to reuse the cells in multiple table views.

So I added some static cells in storyboard and set the custom class in the identity inspector.

But when I want to access the Outlets in the custom view's awakeFromNib method, they are nil. Even later, they're not beeing instantiated.

How can I do this the right way?

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
heyfrank
  • 5,291
  • 3
  • 32
  • 46
  • Give a cell class to table view – Arun sharma Aug 16 '17 at 10:30
  • Could you explain this a little further @Arunsharma – heyfrank Aug 16 '17 at 11:25
  • Just curious... if you are using ***static*** cells, what are you expecting to do with any `@IBOutlets`? Generally, one sets Outlets to manipulate the object at runtime - which makes it no longer static. – DonMag Aug 16 '17 at 12:31
  • I'm talking about `@IBoutlets` to the custom `UITableViewCell` classes. Those cells are added as static to the `UITableView` and have Outlets to the `UITableViewController`. So I could manipulate the views in the cells and reuse the same UI in other table views. – heyfrank Aug 16 '17 at 15:45
  • OK - then what you are describing is not *static* table views... If you have a cell design that you want to use in multiple tables - and you want to manipulate the elements in that cell (even if it's just changing the text of a label) - then you are describing *dynamic* views. You can certainly design your cells in separate xib files and then use them dynamically, in case you were thinking you couldn't. – DonMag Aug 16 '17 at 17:29

2 Answers2

0

If you are adding static cells in the storyboard, they are functioning like separate .xib files and overriding the custom .xibs you are willing to use.

You should use dynamic cells in your table view if you want to reuse your cells in multiple table views. Dynamic cells are usually more sustainable as well, especially in a larger application.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • The point is, that the content isn't dynamic at all. So I thought I could use static cells in storyboard to save time implementing `UITableViewDataSource` logic. – heyfrank Aug 16 '17 at 11:25
0

Ok I figured it out, how this is possible:

I created a container class for my UITableViewCell as it is described in this answer: https://stackoverflow.com/a/34881072/4846592

This class contains a property to get access to the custom cell and loads the XIB in its initializer

Here's the code for Swift 3

class CustomTableViewCellContainer: UIView {    
var cell: CustomTableViewCell!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        cell = Bundle.main.loadNibNamed("CustomTableViewCell", owner: self, options: nil)!.first as! TextFieldTableViewCell
        cell.frame = self.bounds
        cell.autoresizingMask = [.flexibleWidth]
        self.addSubview(cell)
    }
}

Then, I just added a UIView into the ContentView of a static UITableViewCell in the Storyboard and set the height and CustomTableViewCellContainer as custom class name. Finally I added an IBOutlet to it to get access to the custom cell.

heyfrank
  • 5,291
  • 3
  • 32
  • 46