Marvin Louis
Marvin Louis

Reputation: 13

TableView into View of a ViewController Swift functions are not called

When I add A TableViewController everything works fine, because I override the functions. I've made the official apple tutorial and its like my code below.

Did I missed something or is it a bug?

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var data = ["Ferrari","Porsche"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func numberOfSectionsInTableView(tableView: UITableView) -> Int{

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

        return data.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
        cell.textLabel?.text = data[indexPath.row]
        return cell
    }
}

Upvotes: 1

Views: 538

Answers (2)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71852

Drag a tableView in your viewController in storyBoard:

enter image description here

After that connect it's IBOutlet this way:

enter image description here

After that add this code in your viewDidLoad method:

//assign delegate and datasource
tableview.dataSource = self
tableview.delegate = self

//register your tableview cell
self.tableview.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

Change your ReusableCellIdentifier with "cell" into cellForRowAtIndexPath and it will look like:

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

Final code will be:

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var data = ["Ferrari","Porsche"]

    @IBOutlet weak var tableview: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        tableview.dataSource = self
        tableview.delegate = self

        self.tableview.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int{

        return 1
    }

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

        return data.count
    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.textLabel?.text = data[indexPath.row]
        return cell
    }
}

For more Info refer THIS tutorial.

Upvotes: 1

Miknash
Miknash

Reputation: 7948

You are missing delegates and reference for tableView.

Connect tableView with your class and then add the following in the viewDidLoad:

self.yourTableView.delegate = self
self.yourTableView.dataSource = self

And that's it.

You can assign delegate and dataSource through IB as well: https://developer.apple.com/library/mac/recipes/xcode_help-IB_objects_media/Chapters/set_object_delegate.html

Upvotes: 0

Related Questions