Rakesh Mohan
Rakesh Mohan

Reputation: 609

I have created a table view in swift using xib but it shows an error when I run the app

import UIKit

class NewOrdersViewControllers: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var items = ["WE","Heart","Swift"]
    @IBOutlet var tableView:UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

        // Do any additional setup after loading the view.


    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell

        cell.textLabel?.text = self.items[indexPath.row]

        return cell


    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

        print("You selected cell #\(indexPath.row)!")
    }

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

It shows the error when I run the app. I have created a table view in swift using xib but it shows the following error :

NSArgumentException: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7c9a6ed0""

Upvotes: 0

Views: 230

Answers (3)

LearneriOS
LearneriOS

Reputation: 439

I think you have not set the delegate and the datasource for your tableView which happens to come form the xib . This is a common that people when they use UITableView. Set the delegate and the datasource for your tableview using swift syntax for the following objective-c code

self.tableView.delegate = self 
self.tableView.dataSource = self

These statements should be in your viewDidLoad method. Though the first is not compulsory if you dont want to handle the taps from the user on the cells. you have to write the second one for the display of information.

Tell me if something else is required.

Cheers.

Upvotes: 1

Dave Wood
Dave Wood

Reputation: 13333

The error message you're receiving indicates you've set the dataSource property of the tableView to the view, instead of the viewController to which you've added the tableView:numberOfRowsInSection: et al methods.

Upvotes: 0

Sohel L.
Sohel L.

Reputation: 9540

Make sure, you have given DataSource and Delegate to UITableView

tableView.delegate = self
tableView.dataSource = self

You define numberOfRows in 'numberOfSections`. Replace the below code with mine:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count
}

Change to below:

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count
}

Upvotes: 1

Related Questions