Geppelt
Geppelt

Reputation: 363

Empty Cells in TableView with Custom TableViewCell - Swift

I am currently learning Swift programatically. I am wanting to add a tableView to a viewController (for the purpose of being able to manipulate the constraints later) and customize the cells with a TableViewCell.

I can do this with my eyes closed when using the storyboard, but when I try to do it with just straight code I have empty cells.

My storyboard is comprised of one (1) empty viewController that has the custom class of ViewController

I have looked at others with similar issues but non of the solutions have worked. Would love to know what I am overlooking (probably something simple). Thanks in advance for the help!

ViewController.swift:

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var tableView: UITableView = UITableView()
var items: [String] = ["Viper", "X", "Games"]

override func viewDidLoad() {

    tableView.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
    tableView.delegate = self
    tableView.dataSource = self

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

    self.view.addSubview(tableView)
}

func tableView(tableView:UITableView, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
    return 50
}

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

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

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

    //cell.textLabel?.text = self.items[indexPath.row]
    cell.companyName.text = "name"

    return cell
}

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

TableViewCell:

 import UIKit

class TableViewCell: UITableViewCell {

var companyName = UILabel()

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    companyName.frame = CGRectMake(0, 0, 200, 20)

    companyName.translatesAutoresizingMaskIntoConstraints = false

    contentView.addSubview(companyName)
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

Upvotes: 3

Views: 1620

Answers (4)

Florian L.
Florian L.

Reputation: 849

I think awakeFromNib is not called because you do not register a nib but a class. Try this instead:

class TableViewCell: UITableViewCell {
    let companyName = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        // Initialization code
        companyName.frame = CGRectMake(0, 0, 200, 20)
        companyName.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(companyName)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

Upvotes: 0

Bhaumik Desai
Bhaumik Desai

Reputation: 213

Hello @Michael First thing is you should only use awakeFromNib when you are using a .xib(Nib) and in your case you are using custom class without such xib so, you should use

 override init(style: UITableViewCellStyle, reuseIdentifier: String?){
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    companyName = UILabel(frame: CGRectMake(0, 0, 200, 20))
    contentView.addSubview(companyName)

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

also you should initialise your label before using it. this will solve your problem.

Read apple's documentation for subclassing UITableViewCell here.

Upvotes: 4

Abhinav
Abhinav

Reputation: 38142

Checkout how your cell's companyLabel is laid out. Does it exist or no?

In your code, I replaced companyLabel with default textLabel and it worked for me.

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

Upvotes: 0

katleta3000
katleta3000

Reputation: 2494

If you want your custom cell to load from some custom xib you do sometimes like:

tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "CustomTableViewCell")

And you should have CustomTableViewCell.xib file where you have table view cell with reuse identifier CustomTableViewCell

Upvotes: 1

Related Questions