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
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
.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With