6

I have a tabbar view that is connected to some items.

I want that one of these items contains a TableView, but I don't want to use a TableViewController because I want to put something else in the head of the page.

My ViewController implements the 2 protocols UITableViewDataSource and UITableViewDelegate and contains the following functions:

        func tableView(_ tableView: UITableView, numberOfRowsInSection
      section: Int) ->
    Int {
        // Return the number of rows in the section.
        return annunci.count
     }


   func tableView(_ tableView: UITableView, cellForRowAt indexPath:
      IndexPath) ->
    UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCell(withIdentifier:
         cellIdentifier, for: indexPath)
        // Configure the cell...
        cell.textLabel?.text = annunci[indexPath.row]
        return cell }

Then, from the storyboard I added a prototype cell with identifier "Cell" and linked the tableView to the viewController as DataSource and Delegate.

When I run the app it is ok, but when I select the item that contains the table view the app throws this exception:

       *** Terminating app due to uncaught exception   
     'NSInvalidArgumentException', reason: '-[UIViewController   
      tableView:numberOfRowsInSection:
      unrecognized selector sent to instance 0x101c29370'

How can I put something on the top of the page and then the tableView or how can i solve this problem?

I am using Swift 3 and Xcode 8.2.1

Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198

4 Answers4

12

This can happen if you didn't set the view controller's class in the storyboard.

In your storyboard, select the view controller with the UITableView and go to the identity inspector (third tab in the right pane). Make sure the view controller's class is set to your custom class and not UIViewController.

identity inspector

Samantha
  • 2,169
  • 1
  • 12
  • 23
3

For me it was that I had forgotten to include the delegate and data source.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { ... }

For other reasons, see this minimal working version of a UITableView. Run it in a new new project and compare your code to it to find the bug.

Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
1

give the following outlet connection in table view in your view controller. Datasource and delegate

example for following link: https://code.tutsplus.com/tutorials/table-view-basics--mobile-14038

koen
  • 4,535
  • 6
  • 38
  • 77
Vignesh J
  • 217
  • 2
  • 12
0

I ran into this issue in the same scenario as you as well, even after implementing the data source and delegate methods in my view controller, and connecting the storyboard view controller correctly.

In my case initializing self.tableView.tableHeaderView or self.tableView.tableFooterView before setting the tableView.dataSource caused the crash since setting the header view triggers a table view reload.

Pranav Kasetti
  • 4,773
  • 2
  • 16
  • 38