Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableViewCell in Swift programmatically

Tags:

ios

swift

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")
like image 246
Robert Hirsch Avatar asked Jan 02 '15 22:01

Robert Hirsch


People also ask

What is UITableView in Swift?

A view that presents data using rows in a single column.

What is dequeueReusableCell?

dequeueReusableCell(withIdentifier:)Returns a reusable table-view cell object after locating it by its identifier.


1 Answers

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()

like image 173
Paulw11 Avatar answered Oct 04 '22 14:10

Paulw11