PizzaRings
PizzaRings

Reputation: 171

Collapse Expand UITableView

I tried to expand/collapse an UITableView that is inside an UIViewController. I put the UITableView height constraint to zero and make the animation

func collapseExpandRoomSection() {
     isRoomCollapsed = !isRoomCollapsed
     tableHeightConstraint.constant = isRoomCollapsed ? 0.0 : totalTableHeight    
     UIView.animateWithDuration(0.3) { 
       self.view.layoutIfNeeded()
     }
 }

The collapse effect works fine but when I tried to expand the table all the cells are gone.

Thanks

Upvotes: 0

Views: 191

Answers (1)

Zac Kwan
Zac Kwan

Reputation: 5757

Look like you have updated the constraint, You should be doing it in this order I believe:

self.view.setNeedsUpdateConstraints()
self.view.setNeedsLayout()
self.view.layoutIfNeeded()

As you have updated the constraint, therefore you should firstly update the set update constraints and set needs layout. Followed by the layoutIfNeeded() to have the change apply immediately.

As discussed briefly on the comments, the code is only applicable if you override the updateConstraint. Is not correct in answering the question above.

Upvotes: 0

Related Questions