Shrikant Kekane
Shrikant Kekane

Reputation: 15

Custom UITableViewCell created using Storyboard shows no label data - Swift

Problem: I have a simple UITableViewController and want to display a label in a custom made cell. I created a simple UITableViewController with number of sections 1 and rows 4 hardcoded. in storyboard, customised the cell with a new label, gave it an identifier and mapped an outlet in the DemoTableViewCell as well. Below are snapshots of all the different snippets of the code. Still for me the label text assigned in the function is not getting displayed.

class DemoTableViewCell: UITableViewCell {

    @IBOutlet weak var demoLabel: UILabel!

}

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 4
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell : DemoTableViewCell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! DemoTableViewCell

    cell.demoLabel.text = "Override Label"
    // Configure the cell...

    return cell
}

Cell Reuse Identifier

Assigned a Custom Class

Outlet in the custom cell class

Output Screenshot

Storyboard Label

Can someone please let me know what am I doing wrong?

Upvotes: 0

Views: 119

Answers (2)

grow4gaurav
grow4gaurav

Reputation: 3305

Rest all of your code is fine except the autolayouts. You need to apply following constraints to your demoLabel:

enter image description here

enter image description here

Upvotes: 0

aiwiguna
aiwiguna

Reputation: 2922

My assumption is you did not set constraint for that label, because your label is in center of the cell with custom height but in your simulator your cell height is only default height 44 pt

some option to do

  1. you can try to set the label to the top of the cell

  2. set tableview row height in storyboard to equal to your custom cell height enter image description here

  3. the proper step to do is set the label constraint to make the cell height proper with the label position

Upvotes: 1

Related Questions