David DelMonte David DelMonte - 1 month ago 6
Objective-C Question

Swift TableView for iOS

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
}


}

Answer

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!
    }
}
Comments