slimshady
slimshady

Reputation: 23

Wondering how I subclass UITableViewCell in swift 3.0 programmatically?

As the title implies I am trying to customize/subclass the UITableViewCell class in swift 3.0 programmatically and have the custom cells appears in a table.

I have the following subclass of UITableViewCell declared:

import Foundation
import UIKit

class TableViewCellCustom: UITableViewCell {
    var myLabel: UILabel!
    var myButton: UIButton!

    let screenSize = UIScreen.main.bounds

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

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = UIColor.blue

        myLabel = UILabel()
        myLabel.frame = CGRect(x: 180, y: 20.0, width: 50.0, height: 20.0)
        myLabel.textColor = UIColor.black
        myLabel.textAlignment = .center
        contentView.addSubview(myLabel)

    }
}

This should allow me to do the simple case of having a custom cell with a label in a specific location in the cell, of my designation. I am a bit confused though as to how I should integrate this into my ViewController where I am delegating the UITableView.

Currently my ViewController looks like so:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView  =   UITableView()

    var items: [String] = ["Party at Dougs", "Pillage at the Village", "Meow party at sams"]

    let screenSize: CGRect = UIScreen.main.bounds

    var appTitle: UIView! = nil
    var appTitleText: UILabel! = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height

        tableView.frame         =   CGRect(origin: CGPoint(x: 0,y :screenHeight*0.12), size: CGSize(width: screenWidth, height: screenHeight*0.88))
        tableView.delegate      =   self
        tableView.dataSource    =   self

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

        self.view.addSubview(tableView)

    }

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "td")


        cell.textLabel?.text = items[indexPath.row]


        self.tableView.rowHeight = screenSize.height * 0.15
        return cell
    }

}

I think the core of my problem is being unsure how to connect my subclass of UITableViewCell to my TableView. How can I do this? thanks.

Upvotes: 1

Views: 3479

Answers (1)

Annie Gupta
Annie Gupta

Reputation: 2822

For custom cell created programmatically, without using xib, which is your case currently, you can use the below code -

instead of this

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

use this line

tableView.register(TableViewCellCustom.self, forCellReuseIdentifier: "cell")

Also instead of line

let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "td")

use this line

let cell = TableViewCellCustom(style: UITableViewCellStyle.default, reuseIdentifier: "td")

For when using xib to create custom cell . Use the below code in your UITableViewDataSource cellForAtIndexPath if you have xib for custom cell.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("td") as? TableViewCellCustom
    if cell == nil {
         tableView.registerNib(UINib(nibName: "TableViewCellCustom", bundle: nil), forCellReuseIdentifier: "td")
         cell = tableView.dequeueReusableCellWithIdentifier("td") as? TableViewCellCustom
     }
        cell.textLabel?.text = items[indexPath.row]


        self.tableView.rowHeight = screenSize.height * 0.15
        return cell
    }

Upvotes: 1

Related Questions