How to programmatically add reusable custom views? (Swift)

I created a reusable xib file containing a table and loaded into a file TableView.swiftas follows:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)    
    Bundle.main.loadNibNamed("TableView", owner: self, options: nil)
}

I only mention this to clarify that I am not confused about how to download the xib file


I can easily load a reusable view into my file RootViewController.swiftby adding the view to UIViewControllerand providing that view with a custom class as follows:

enter image description here

and then create an output for this view as follows:

enter image description here

So here is my question:

Why can't I just add the view as follows:

let tableViewView = TableView()

When I do this, I get an error message that I don't quite understand:

enter image description here

+4
1

.

, TableView UITableView, :

class TableView: UITableView {

    override init(frame: CGRect, style: UITableViewStyle) {
        super.init(frame: frame, style: style)
        // any additional setup code
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // any additional setup code
    }

}

, , .

+2

Source: https://habr.com/ru/post/1676082/


All Articles