0

I am working on a project on XCode 6 and Swift. I need to add a UITableView to a UIViewController, and then fill the table with content.

So, on my default UIViewController, I added a UITableView and a UITableViewCell (using the storyboard). Next, I set the UIViewController as the dataSource and delegate for the table.

Finally, I added this code to my view controller.swift:

import UIKit

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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


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

    return 1


}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    return 3
}



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

     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = "Hello world"

    table.reloadData()

    return cell


}

However, as I run the project, I get a crash with the following log

2015-04-08 21:06:52.820 tableViewTest[12380:783945] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "BYZ-38-t0r-view-8bC-Xf-vdC" nib but didn't get a UITableView.'

I've tried anything, I really don't know how to solve this.

Can anyone help me?

Vig
  • 1,532
  • 1
  • 12
  • 28
dpstart
  • 1,018
  • 1
  • 10
  • 25
  • cell.textLabel?.text = "Hello world" table.reloadData() --> you don't have to reload, I guess this causes a recursive call and thus crashes – Vig Apr 08 '15 at 19:01
  • I've tried, but actually it crashed anyway. @Vig – dpstart Apr 08 '15 at 19:07
  • what's your console output – Vig Apr 08 '15 at 19:08
  • 2015-04-08 21:06:52.820 tableViewTest[12380:783945] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "BYZ-38-t0r-view-8bC-Xf-vdC" nib but didn't get a UITableView.' @Vig – dpstart Apr 08 '15 at 19:08
  • That error is saying that you have a UITableViewController (that's what your code says too), but you're telling us you're using a UIViewController. You seem to be confused as to what you're actually doing. – rdelmar Apr 08 '15 at 19:10
  • class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate, I don't think you need UITableViewController in your protocol, try removing them and I assume your tableview outlet has delegate and datasource connected to this viewcontroller – Vig Apr 08 '15 at 19:13
  • UITableViewDataSource and UITableViewDataSource seem to need UITableViewController to work. @Vig – dpstart Apr 08 '15 at 19:16
  • I need to control the UITableView inside the UIViewController. Are there others ways to do this? I actually need a page like the Instagram profile page. @rdelmar – dpstart Apr 08 '15 at 19:17
  • You don't need the UITableViewController, you need UIViewController. So it should be ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource – Vig Apr 08 '15 at 19:18
  • Great consider up voting or approving the answer – Vig Apr 08 '15 at 19:21

2 Answers2

1

You have a tableview as IBOutlet and you need your ViewController to be a subclass of UIViewController.

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {}
Vig
  • 1,532
  • 1
  • 12
  • 28
  • 1
    It shouldn't be "conform" to UIViewController; UIViewController is not a protocol. It should read "you need ViewController's superclass to be UIViewController". – rdelmar Apr 08 '15 at 19:25
  • Ah yes, how did I miss that – Vig Apr 08 '15 at 19:25
0

Seems to me that you're using an UIViewController in the storyboard but referencing an UITableViewController in the code. Try changing

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

to

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

and put table.reloadData() in the viewDidLoad function.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
Nahum G
  • 125
  • 1
  • 9