How to fix “nib but didn't get a UITableView”

2019-01-26 13:18发布

问题:

Why I get this error?

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "pB1-re-lu8-view-o7U-YG-E7m" nib but didn't get a UITableView.'

here is my code:

class FriendListTableViewController: UITableViewController{


var objects = NSMutableArray()
var dataArray = [["firstName":"Debasis","lastName":"Das"],["firstName":"John","lastName":"Doe"],["firstName":"Jane","lastName":"Doe"],["firstName":"Mary","lastName":"Jane"]]

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

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table View

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataArray.count
}

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    let object = dataArray[indexPath.row] as NSDictionary
    (cell.contentView.viewWithTag(10) as! UILabel).text = object["firstName"] as? String
    (cell.contentView.viewWithTag(11) as! UILabel).text = object["lastName"] as? String
    return cell
}

 override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return false
}

my storyboard is like this:

回答1:

I have face same issue once upon time, and So stupid mistake it was, I have subclass the UITableViewController, where I have added UITableView in UIViewController

From your storyboard Image, it may be solved if you use UIViewController instead of UITableViewController, Just try that way can solve your issue,like

class FriendListTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 


回答2:

SWIFT 2 UPDATE

I tried @Viralsavaj's answer but it didn't work for me until I "hooked up" the tableView as an outlet from the storyboard in the ViewController.

like this: @IBOutlet weak var tableView: UITableView!

Also add this to your class as @Viralsavaj mentioned:

class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

I used specifically this link help as well: How to reference tableView from a view controller during a segue

Just drag your tableView into the ViewController and make it an outlet. Then you can reference its properties such as self.tableView.reloadData().

This property as well as others that referenced the tableView were giving me errors until I added the tableView as a referencing outlet.

Hope this helps those in the future.



回答3:

I had this issue with Swift 2, Xcode 7.2, I changed a View Controller I dragged to my Storyboard to a custom UITableViewController class, I then dragged a Table View onto the View Controller. I didn't realize I placed it as a child of the View that was part of the original View Controller I dragged onto the Storyboard.

I simply deleted the View and added the Table View again as the first child of the Super View.