2

I know this is dublicate question (and extremely sorry), but I've tried everything I've found on SO so far, but I am still receiving error this error:

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

Let me clarify the parts clearly with images, so we can produce a good answer for solving related problem.

This is the folder view (ViewController - main view, and TableView1 is child):

enter image description here

My TableView1.xib file has:

enter image description here . enter image description here

I have also edited my TableView1.swift:

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

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

In my Main.Storyboard:

When I select View Controller enter image description here (enter image description here) it looks like this:

    1. enter image description here
    1. enter image description here

However, when I clicked on the View (enter image description here):

    1. enter image description here
    1. enter image description here

What am I doing wrong or missing on the way?

More Info:

enter image description here

enter image description here

senty
  • 12,385
  • 28
  • 130
  • 260
  • who is owner of TableView1.xib, it must be TableView1.swift – maddy Nov 18 '15 at 19:54
  • What class is TableView1.swift? Probably the file in the .xib is supposed to be set to that class. – vadian Nov 18 '15 at 19:55
  • @vadian That's right. `TableView1.swift` and it's `.xib` should be the child of `ViewController.swift` & `Main.storyboard` – senty Nov 18 '15 at 19:55
  • select tableview1.xib and locate owner on top left – maddy Nov 18 '15 at 19:56
  • @Alok, is it tableview.xib > File's Owner > and? . It's custom class > class is TableView1 and Module > Current.. – senty Nov 18 '15 at 19:58
  • now in attribute inspector there are some tabs (i am missing name). it looks like custom class, module same as you have attached in your question screen shot – maddy Nov 18 '15 at 19:59
  • @Alok tableview.xib > File's Owner > custom class > class is TableView1 and Module > Current.. – senty Nov 18 '15 at 20:00
  • can you just break connection in xib for data source and delegate, just break it and run see if crashes still – maddy Nov 18 '15 at 20:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95486/discussion-between-alok-and-senty). – maddy Nov 18 '15 at 20:04
  • I found another cause for this issue in my answer [here](https://stackoverflow.com/a/65619591/4995828). – Pranav Kasetti Jan 07 '21 at 20:52

3 Answers3

1
-[UIViewController tableView:numberOfRowsInSection:]:

So as per the error log you have somewhere type mismatch when you set deletegate and datasource.Its happening because on xib you say the tableview1.xib should implement datasource and delegate.But in ViewController1.swift you are mistakenly referencing to UIViewController. now you see UIViewController is just unknown about implementing delegate and datasource.As it dont find (obviously) it crashes. so:

let controller : UIViewController = UIViewController(nibName: "TableView1", bundle: nil)

changed to:

let controller : UITableViewController = UITableViewController(nibName: "TableView1", bundle: nil)

should work.

maddy
  • 4,001
  • 8
  • 42
  • 65
0

Did you call this function else where?

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

}

It looks like this function being called besides the delegate.

Mengli
  • 11
  • 4
0

No clue about Swift, but ObjC on iOS...

The Exception says that the class UIViewController can't handle the message [UIViewController tableView:numberOfRowsInSection:]. This indicates, that at some point, the dataSource is changed to "super".

Do you need the "override"? You're not overriding a message implementation, since UIViewController doesn't implement one.

Anticro
  • 685
  • 4
  • 12