0

I'm trying to display a table view inside a container view, which itself is a part of a view controller. Here's my HomeScreenViewController, which has a button that segues to the BroadcastSearchViewController.

Home Screen View Controller

Once I click the button, however, I'm getting the following runtime error. Why? What can I do to make this work? Do I need to switch up my design somehow?

Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: '-[UITableViewController loadView] instantiated view controller with identifier 
"UIViewController-OSu-KC-K41" from storyboard "Main", but didn't get a UITableView.'

Here's what my BroadcastSearchViewController looks like in Storyboard. It's presented modally from clicking the search button.

enter image description here

And here's what my table view controller, which is embedded in the container view, looks like:

enter image description here

BroadcastSearchViewController.swift

class BroadcastSearchViewController : UIViewController {

    @IBOutlet weak var broadcastSearchBar: UISearchBar!
    @IBOutlet weak var broadcastSearchSegmentedControl: UISegmentedControl!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

BroadcastSearchResultsTableViewController.swift

class BroadcastSearchResultsTableViewController : UITableViewController {

    let datasource = BroadcastSearchResultsTableViewDataSource()

    override func viewDidLoad() {
        datasource.broadcasts = [
            Broadcast(
                song: Song(id: "1", title: "November Rain", artist: "Guns N' Roses"),
                user: User(username: "sylphrenetic")
            ),
            Broadcast(
                song: Song(id: "2", title: "How To Disappear Completely", artist: "Radiohead"),
                user: User(username: "sylphrenetic")
            )
        ]
        self.tableView.dataSource = datasource
        self.tableView.reloadData()
    }

}
Tyler Cheek
  • 327
  • 2
  • 14

1 Answers1

0

I ended up figuring it out from this answer on a different question. Following those steps, I realized I needed to remove the default view controller and replace it with a new table view controller manually, instead of just setting the view controller to be my table view controller. This was confusing at first but it makes sense now that you can't have a UITableView inside of a UIView.

Tyler Cheek
  • 327
  • 2
  • 14
  • 2
    You can have a UITableView inside a UIview, but not if the view controller is a UITableViewController. If it is then the root view must be a tableview. – Paulw11 May 30 '20 at 05:24