kiwidude89
kiwidude89

Reputation: 63

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

I am very new to Swift as I justed started on it only a few months back. I have an aching problem with this error.

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

Here is a list of possible fixes that I have so far gathered and implemented already.

  1. I have changed the Custom Class to CheckboxQuestion class instead of the UIViewController class.

Custom Class

  1. I have connected the tableview to the datasource and delegate.

  2. I have accounted for all loose referencing outlets.

  3. I have named the reusable identifier outlet for each prototype cell.

prototype cell 1

After all those, I am still getting the error above.

The breakpoint of the exception error happened in the appdelegate class at this line

class AppDelegate: UIResponder, UIApplicationDelegate {

Is there something else which I am missing?

Here is the code for CheckboxQuestion Class code

import UIKit

var checkboxCellInfo = ["Field 1", "Field 2"]

class CheckboxQuestion: UIViewController {

@IBOutlet weak var tableView: UITableView!

//1. determine number of rows of cells to show data
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {  
    return checkboxCellInfo.count + 1
}

//2. inputs info into each cell from array 'cellInfo'
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell? {

    if indexPath.row <= checkboxCellInfo.count - 1 {

        let cell:CheckboxQuestionCellConnect = self.tableView.dequeueReusableCellWithIdentifier("CheckboxNumber") as! CheckboxQuestionCellConnect

        return cell 

     } else {

        let cell:CheckboxQuestionCellConnect = self.tableView.dequeueReusableCellWithIdentifier("CheckboxAdd") as! CheckboxQuestionCellConnect

        return cell

    }  
}

//3. determines height of each cell
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 60
}

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

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

Upvotes: 1

Views: 10004

Answers (10)

RohitK
RohitK

Reputation: 1494

In my case, I have not connected the ViewController in storyboard to my custom class

Upvotes: -1

drodriguez
drodriguez

Reputation: 380

make sure you have this in your controller.

extension NameController: UITableViewDelegate { // code... }

extension NameController: UITableViewDataSource { // code... }

Upvotes: 1

Abhinav Dobhal
Abhinav Dobhal

Reputation: 630

In my case, I had provided: 1. all the outlets right, 2. datasource and delegate right,

The thing i was missing was: I hadn't provided the class and storyboard identifier for UIViewController in the storyboard. hence it shows

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

because a normal UIViewController does not have these methods, when we conform to UITableViewDatasource, we provide it in our custom UIViewController class.

So please check the identity inspector for UIViewController in the storyboard and provide the proper class there.

Consider the following image:

enter image description here

Upvotes: 1

Hafiz Anser
Hafiz Anser

Reputation: 581

Sometimes you are not connected your file with the storyboard.

Upvotes: 0

Aviv
Aviv

Reputation: 14467

You probably missed UITableView protocol implementation, Make sure you implemented the protocols you're required to implement.

  1. UITableViewDelegate
  2. UITableViewDataSource

add protocols implementations on h file:

@interface YourClass : UIViewController <UITableViewDelegate,UITableViewDataSource>

@end

add required methods on m file:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Upvotes: 1

Gowtham Sooryaraj
Gowtham Sooryaraj

Reputation: 3907

sometimes you add a same class name in different viewcontroller. one of these viewcontroller have a tableview.

Upvotes: 0

Vikas Rajput
Vikas Rajput

Reputation: 1874

Make sure you have added below.

class ViewController: UIViewController 
      ,UITableViewDataSource,UITableViewDelegate{

And also connected the delegate and datasource to tableview in stoaryboard.

Upvotes: 4

Devendra Agnihotri
Devendra Agnihotri

Reputation: 253

please ensure you deleted outlets from code but not from storyboard.just right click on tableview from storyboard .delete outlets and reconnect it.May be you have added two outlets for same tableview.

Upvotes: 7

Chirag kukreja
Chirag kukreja

Reputation: 433

I think in this case you may have forget to assign tableview delegate and datasource to self.

self.tableView.dataSource = self

self.tableView.delegate = self

Upvotes: 2

Guido Hendriks
Guido Hendriks

Reputation: 5776

You're implementing UITableViewDelegate/UITableViewDataSource methods, but it doesn't seem like your class conforms to either of them. Since you're inheriting from UIViewController, you'll need to add those protocols and set the delegate and dataSource of the table view to self:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

If the view controller only shows a view controller, you should consider using a UITableViewController instead of UIViewController, then the stuff above doesn't apply - you get that for free with a UITableViewController. But I can't tell from code if this is the case.

Upvotes: 3

Related Questions