1

I have referred to this question, and tried to implement the various answers. I'm doing something dumb, as cellForRow is not being called, and I'm just getting a blank tableView. Are there other data source or delegate methods that I must override?

Thanks

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view, typically from a nib.
        //tableView.delegate = self
        //tableView.dataSource = self

        tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")

        //tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
        tableView.reloadData()
    }

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

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
        println("cellForRowAtIndexPath")
        println()
        //variable type is inferred
        var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

        if !cell {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")

        }

        cell!.textLabel.text = "Text Label"
        cell!.detailTextLabel.text = "Detail Text Label"

        return cell
    }


}

UPDATE - The following appears to work.. Thanks for the help!!

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view, typically from a nib.
        //tableView.delegate = self
        //tableView.dataSource = self

        tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")

        //tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
        tableView.reloadData()
    }

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

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

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
        println("cellForRowAtIndexPath")
        println()
        //variable type is inferred
        var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

        if !cell {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")

        }

        cell!.textLabel.text = "Text Label"
        cell!.detailTextLabel.text = "Detail Text Label"

        return cell
    }


}
Community
  • 1
  • 1
ICL1901
  • 7,632
  • 14
  • 90
  • 138
  • 1
    What about numberOfRowsForSection? – Stavash Jun 11 '14 at 15:20
  • Is the `dataSource` of your tableView set to this class? Check your `tableView.dataSource` and make sure its there – Jack Jun 11 '14 at 15:22
  • 1
    your class has to conform the `UITableViewDataSource` and `UITableViewDelegate`protocols, and has to implement all required methods, as previously, which is pretty much about two methods which are related to the `dataSource` delegate only. there is nothing new here. – holex Jun 11 '14 at 15:23
  • You are all correct. I missed all three~ For others, I'm updating my code with the working set.. Note that I'm using a TableViewController, rather than a ViewController, with a tableview added. – ICL1901 Jun 11 '14 at 15:27
  • Someone want to check what I've done, and write the answer? – ICL1901 Jun 11 '14 at 15:30
  • Why is this part: `//tableView.delegate = self //tableView.dataSource = self` commented out? – Lord Zsolt Jun 11 '14 at 15:35
  • They are connected in interface builder – ICL1901 Jun 11 '14 at 15:43

2 Answers2

3

Here is working code. No need to register cell in -viewDidLoad(). Also UITableView is added as subview in Storyboard.

If someone has this kind of requirement.

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
        self.tableView.dataSource = self
    }

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

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

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

        var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as UITableViewCell?

        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
        }

        cell?.textLabel?.text = NSString(format: "%d", indexPath.row) as String
        return cell!
    }
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
1

You need to connect Delegate DataSource of TableView, both are connected with the tableview then can only NumberOfRowsInSection and CellForRowAtIndexpath called.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55