Wednesday, March 9, 2016

How to force uitableview to resize a cell (after deactivating a height constraint)?

Leave a Comment

I am trying to put an extendable UITableViewCell by tapping on a button.

I am using the constraints to force the dynamic UITextView to tell the UITableViewCell the its new height. (using autolayout and UITableViewAutomaticDimension)

This works as expected without the button: The height of the UITableViewCell depends on the UITextView's height.

Having a constraint on the UITextView to maximize the size of the cell, when tapping the button (the cell is collapsed), I want to remove height constraint to the UITextView (that is limiting the cell height) and update the cell accordingly. Here's the extend method:

-(void)extendCellWasTapped:(UITableViewCell*)senderCell{      MAFollowingPostTableViewCell *cell = (MAFollowingPostTableViewCell*)senderCell;      NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];      if (![self.arrayExtendedTitlesAtIndexPaths containsObject:indexPath]) {         [self.arrayExtendedTitlesAtIndexPaths addObject:indexPath];     }      if ([self.tableView.indexPathsForVisibleRows containsObject:indexPath] ) {         dispatch_async(dispatch_get_main_queue(), ^(void){              [NSLayoutConstraint deactivateConstraints:@[cell.constraintTextViewHeight]];             // CGPoint offset = self.tableView.contentOffset;              [self.tableView beginUpdates];             [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];             [self.tableView endUpdates];  // I would like to minimize the animations as well here.. but thats another //problem //            [self.tableView.layer removeAllAnimations]; //            [self.tableView setContentOffset:offset animated:NO]; //            [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];          });     } } 

If I tap the button and then scroll to a similar cell, it is already expanded. So I am missing something here. I already tried to do [cell updateConstraints] after deactivating the constraint, but it doesn't work.

UPDATE

At first, the button doesn't show up for some reason. And I noticed that (with the same code) if I scroll down and then up, the button shows up and, if I try to extend it, it works.

UPDATE

This is my UIViewController :

http://www.gfycat.com/FairBossyAfricanporcupine

Notice that the extend button is not there, but once I scroll down/up it comes shows up and it extends the cell once I tap on the extend button.

What am I missing here?

4 Answers

Answers 1

To do such a thing, you shouldn't rely on constraint, but on tableView(_:heightForRowAtIndexPath:) you just have to give the right height depending on the context.

Pushing your button would trigger a change so tableView(_:heightForRowAtIndexPath:) returns a different value for your cell that you extend.

And just after doing this trigger, just do:

[self.tableView beginUpdates]; [self.tableView endUpdates]; 

without anything between them, it will trigger the "natural animation" of UITableView and do exactly what you want

Answers 2

Are forgetting to remove indexpath from self.arrayExtendedTitlesAtIndexPaths ?

if (![self.arrayExtendedTitlesAtIndexPaths containsObject:indexPath]) {     [self.arrayExtendedTitlesAtIndexPaths addObject:indexPath]; } else {    [self.arrayExtendedTitlesAtIndexPaths removeObject:indexPath]; } 

Answers 3

If the button isn't there and while you scrolling it show again surely you have a problem with reusability.

You're probably decide about to show or hide button in cellForRowAtIndexPath:. If you have a condition there try to show this button always, and if this will work as expected then hide a button inside a cell whenever this constraint is there or not. The right method is prepareForReuse inside UITableViewCell subclass.

Answers 4

Call [cell layoutSubviews]; once your changes are completed.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment