Search code examples
iosswiftuitableviewautolayoutnslayoutconstraint

Controlling Constraint of Cell Item from TableViewController


I have a TableViewController with dynamic cells containing 3 labels and an imageView inside it. I created it in a Storyboard. It looks like this...

      - Label1
 oImg - Label2
      - Label3

And, Label2 is vertically centered to the imageView (call it constraint-A); and the other labels are aligned to Label2, too.

What I am trying to do is, in cellForRowAtIndexPath in TableViewController, check if Label3 is empty, move Label2 down and Label1 keeping the constraint set before (so also move down).

As first option, I tried connecting the constraint-A to TableViewController,but as it it is TableViewCell, I am unable to connect the constraint outlet to TableViewController.

Then, I tried manually moving them down, but weirdly it's only affecting one row (first one without Label3) whereas rest of the cellForRowAtIndexPath works okay..

cell.Label2.frame.origin.y += 5
cell.Label1.frame.origin.y += 5

What is the appropriate way of achieving this? (So I can either control the constraint-A or move Label2 manually and Label1 follows)


Solution

  • Create IBOutlet property for y-cord constraint of label1 and label2 and then you change "your property name".constraint = "some value" rather than changing the frame y-cord.