Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 843
Amogh Talpallikar Avatar asked Nov 02 '22 18:11

Amogh Talpallikar


2 Answers

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.

like image 107
Andrew Avatar answered Nov 09 '22 10:11

Andrew


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.

like image 39
Prashant Nikam Avatar answered Nov 09 '22 11:11

Prashant Nikam