Reputation: 15
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
}
Can someone please let me know what am I doing wrong?
Upvotes: 0
Views: 119
Reputation: 3305
Rest all of your code is fine except the autolayouts. You need to apply following constraints to your demoLabel:
Upvotes: 0
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
you can try to set the label to the top of the cell
set tableview row height in storyboard to equal to your custom cell height
the proper step to do is set the label constraint to make the cell height proper with the label position
Upvotes: 1