简体   繁体   中英

Custom UITableViewCell in Swift programmatically

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM