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
    }
}
2
  • 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. Commented Jul 1, 2017 at 2:44
  • It is shown in the image that UITableView is the entry point but UItabbar controller should be. Commented Jul 1, 2017 at 3:56

2 Answers 2

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
}

}

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

1 Comment

I was looking to use swift built in function, instead of defining a separate function and calling it.
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
    }
}

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.