0

I have followed this stackoverflow question & answer as guide to create a custom cell inside table view (without adding a prototype cell in the storyboard), However I was not able to create a table view with the custom cells, I just get rows of empty cells. I did look at other SO questions but they did not help.

I believe I am doing something wrong with the way I register the cell. (FYI: When I add a prototype cell to my table view and assign it a class and cell id and if I then disable tableView.register(AppointmentTVCell.self, forCellReuseIdentifier: cellId) it works, I am not interested in this since I want to minimise the use of IB)

Below is my tableViewController in storyboard, to which a table view class AppointmentsTVC has been assigned enter image description here

Below is my code for the Table View Controller

class AppointmentsTVC: UITableViewController {

    let cellId = "reuseIdentifier"

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(AppointmentTVCell.self, forCellReuseIdentifier: cellId)
    }

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

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 2
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("Cell for Row at func called")
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! AppointmentTVCell  // Creating a cell
        cell.caseId.text = "123456"
        return cell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 150.0
    }
}

Below is my code for the custom table view cell

class AppointmentTVCell: UITableViewCell {

    let caseId: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.frame = CGRect(x: 0, y: 50, width: 100, height: 30)
        label.numberOfLines = 2
        //label.text = "Hello"
        label.textColor = UIColor.black
        return label
    }()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        contentView.addSubview(caseId)

    }

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

        // Configure the view for the selected state
    }
}
SpaceX
  • 2,814
  • 2
  • 42
  • 68
  • Is the problem because I am adding subview in `awakeFromNib` function, I came to know that `awakeFromNib()` will not be called unless there is a nib file. – SpaceX Jul 01 '17 at 02:44
  • It is shown in the image that UITableView is the entry point but UItabbar controller should be. – Fattaneh Talebi Jul 01 '17 at 03:56

2 Answers2

1

use the didMoveToSuperview method

class AppointmentTVCell: UITableViewCell {

let caseId: UILabel = {
    let label = UILabel()
    label.font = UIFont.systemFont(ofSize: 14)
    label.frame = CGRect(x: 0, y: 50, width: 100, height: 30)
    label.numberOfLines = 2
    //label.text = "Hello"
    label.textColor = UIColor.black
    return label
}()
override func didMoveToSuperview() {
    super.didMoveToSuperview()
    if superview != nil {
    contentView.addSubview(caseId)
    }
}

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

    // Configure the view for the selected state
}

}

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
  • I was looking to use swift built in function, instead of defining a separate function and calling it. – SpaceX Jul 03 '17 at 09:57
0

I have come to know that without a NIB file, awakeFromNib won't be invoked, so the view wasn't being added.

By looking at this Stackoverflow question I modified the code to the below and it worked.

class AppointmentTVCell: UITableViewCell {

    var caseId = UILabel()

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


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

        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //Do your cell set up
        caseId.frame = CGRect(x: 0, y: 50, width: 100, height: 30)
        caseId.text = "Hello"
        caseId.font = UIFont(name:"HelveticaNeue-Bold", size: 16)
        contentView.addSubview(caseId)
    }


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

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

        // Configure the view for the selected state
    }
}
SpaceX
  • 2,814
  • 2
  • 42
  • 68