2

I'm added in two labels into a table cell, the top one being left justified and the bottom being right justified, however the text isn't displaying properly.

I added the proper constraints for the tableview in storyboard but that hasn't resolved the issue. Here is the code i used to set up the labels and the resulting screenshot, I'm only using the borders for debugging purposes

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)


    var label1 = UILabel(frame: CGRectMake(0,0, cell.bounds.width, cell.bounds.height/3))
    var label2 = UILabel(frame: CGRectMake(0,cell.bounds.height/3, cell.bounds.width, cell.bounds.height/3 * 2))



    label1.textAlignment = .Left
    label1.text = "AAAA"
    label1.layer.borderColor = UIColor.blackColor().CGColor
    label1.layer.borderWidth = 1.0


    label2.textAlignment = .Right
    label2.text = "BBBB"
    label2.layer.borderColor = UIColor.redColor().CGColor
    label2.layer.borderWidth = 1.0


    cell.addSubview(label1)
    cell.addSubview(label2)

enter image description here

edit: adding in new picture to show constraints

enter image description here

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
hnaderi
  • 409
  • 8
  • 16

2 Answers2

2

Remove constraints to margin when pinned all you edges ..

For more guide about constraints to margin, check this link

Community
  • 1
  • 1
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
1

You don't have any constraints on the cell itself. I'd recommend that you subclass UITableViewCell, add the two labels to the custom cell, set up constrains, and then modify those labels in tableView.dequeueReusableCellWithIdentifier. Then you'll keep constraints as set up in storyboard.

As it currently exists, you are creating new labels each time a cell is loaded, so you would have to create constrains programmatically as well, which is not ideal.

sschale
  • 5,168
  • 3
  • 29
  • 36