Hightower98 Hightower98 - 5 months ago 98
Swift Question

UITableView unrecognized selector sent to instance

I'm confused on what is going on I'm doing a tutorial on collection views as I'm fairly new to programming, and I believe I'm doing everything right but I get hit

-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
.

Here is the tutorial:
http://www.thorntech.com/2015/08/want-your-swift-app-to-scroll-in-two-directions-like-netflix-heres-how/

import UIKit

class ViewController: UIViewController, UITableViewDataSource {

var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"]

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return categories.count
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! CategoryRow
return cell
}

Answer

Have you set Tableview delegate to UITableViewDataSource using storyboard or programmtically.?

set Tableview delegate to UITableViewDataSource programmtically.

yourtableview.dataSource=self

or

using storyboard set Tableview delegate to UITableViewDataSource show below image.

enter image description here

Or

check if you set UITableViewDataSource wrong to any other view.