Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

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

like image 334
Stefano Miceli Avatar asked Apr 12 '17 23:04

Stefano Miceli


3 Answers

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

like image 137
Samantha Avatar answered Oct 28 '22 20:10

Samantha


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.

like image 45
Suragch Avatar answered Oct 28 '22 19:10

Suragch


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

like image 1
Vignesh J Avatar answered Oct 28 '22 21:10

Vignesh J