Amogh Talpallikar
Amogh Talpallikar

Reputation: 12184

How to increase cell height and subview height at the same time with animation?

I have a custom cell with a subview inside it which on getting content from server increases in height once the response comes.

So I increase the height of this subview with animation, and on completion of the animation I send a delegate message to viewController.

in this delegate I set a instance variable with new value for the cell height and call

[tableView beginUpdates];
[tableView endUpdates];

after that which just resizes the cell with new height with smooth animation. but since this delegate method is called in the completion block of previous animation, it happens after it.

Is there a way to make both of them happen together smoothly ?

Upvotes: 5

Views: 667

Answers (3)

Prashant Nikam
Prashant Nikam

Reputation: 2251

Use NSLayoutConstraint..so that when a cell changes its height and width automatically its subview will also change its height and width.

NSLayoutConstraint *myConstraint1 = [NSLayoutConstraint constraintWithItem:cell
                                     attribute:NSLayoutAttributeWidth
                                     relatedBy:NSLayoutRelationEqual
                                     toItem:subView
                                     attribute:NSLayoutAttributeWidth
                                     multiplier:1.0
                                     constant:-5];

NSLayoutConstraint *myConstraint2 = [NSLayoutConstraint constraintWithItem:cell
                                    attribute:NSLayoutAttributeHeight
                                    relatedBy:NSLayoutRelationEqual
                                    toItem:subView
                                    attribute:NSLayoutAttributeHeight
                                    multiplier:1.0
                                    constant:-5];

[cell addConstraint:myConstraint1];
[cell addConstraint:myConstraint2];

Adjust the constant according to your need.

Hope this will help you.

Upvotes: 1

Andrew
Andrew

Reputation: 1106

Just

1) change the subview height directly without animation,

2) set the new cellHeight of the cell in heightForRowAtIndexPath,

and

3) update the table with animation. If do so, both the subview and the cell will animation correctly.

Upvotes: 1

Misha Vyrko
Misha Vyrko

Reputation: 1000

You ought to make animation in 1 animation function. When you receive new height start animation(you can use layers transformation)

Upvotes: 0

Related Questions