5

I'm looking to programmatically create a UITableView and corresponding custom UITableViewCell in Swift. The table view is working great, but it doesn't seem like the cell labels are instantiating - they come back as nil.

I also don't know how to refer to the content view size when sizing elements.

UITableViewCell

import UIKit

class BusUITableViewCell: UITableViewCell {

    var routeNumber: UILabel!
    var routeName: UILabel!

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

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


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        routeName = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 50)) // not sure how to refer to the cell size here

        contentView.addSubview(routeNumber)
        contentView.addSubview(routeName)
    }

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

        // Configure the view for the selected state
    }

}

UITableView delegate and source

import Foundation
import UIKit

class BusUITableView: NSObject, UITableViewDelegate, UITableViewDataSource {

    var routeService: RouteService = RouteService()

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

        var busRoutes: [Route] = routeService.retrieve()
        return busRoutes.count
    }

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

        var cell:BusUITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as BusUITableViewCell

        var busRoutes: [Route] = routeService.retrieve()

        cell.routeName.text = "test"  // test string doesn't work, returns nil
        return cell
    }

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


    }

}

View Controller

    mainTableView.registerClass(BusUITableViewCell.self, forCellReuseIdentifier: "cell")
0

2 Answers 2

5

If you aren't linking to a prototype cell in a storyboard then you need to register the class for your cell against your tableView using registerClass(_ cellClass: AnyClass, forCellReuseIdentifier identifier: String)

In your case you would use something like this

  tableview.register(BusUITableViewCell.self, forCellReuseIdentifier:"cell")

Also, without a NIB file, awakeFromNib won't be invoked.

Edit: .registerClass() has been renamed to .register()

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry...already had that line in my view controller. Still doesn't work.
I don't see where you actually instantiate the UILabel instance for routeName
Have you set a breakpoint in awakeFromNib? Does it actually get called without a nib file?
Ah...seems as though awakeFromNib isn't called if there is no nib file. I instantiated the routeName in the init block and it worked!
1

Considering you are doing this "programmatically" (no nib file), you're awakeFromNib() will never invoke. You can create a nib, and link it to this backing file, and still create and use the cell programmatically. Or ... choose a different place (like the initializer) to create and add subviews to the cell.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.