2

I am trying to animate a tableview on a view that uses autolayout.

I am able to animate the tableview fine but for some reason there is a unwanted sideways animation when doing so.

The first animation closes the top tableview works fine. However the second animation that extends the bottom tableview is the one that has the sideways movement. Here is my code:

[self.dataSelectionTable layoutIfNeeded]; 
[self.comparatorSelectionTable layoutIfNeeded];

[UIView animateWithDuration:0.5 animations:^{
    self.dataSelectionTableHeight.constant = 44;
    [self.view layoutIfNeeded]; and then captures all of the frame changes
} completion:^(BOOL finished){
    [UIView animateWithDuration:0.5 animations:^{
       self.comparatorSelectionTableHeight.constant = 200; 
       [self.view layoutIfNeeded];
    }];
}];

I have tried changing the animation type but that does not seem to work.

Here is a link to a gif that shows the unwanted sideways animation: http://makeagif.com/YubmP2

Edit

Here is the code for populating the tableviews:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0];
    }

    if (tableView == self.dataSelectionTable) {
        cell.textLabel.text = self.dataSelectionTitles[indexPath.row];
    }
    else {
        cell.textLabel.text = [self.comparatorList[indexPath.row] name];
    }

    return cell;
}
pls
  • 1,522
  • 2
  • 21
  • 41
  • Hm, interesting... What happens if you remove all the "layoutIfNeeded"s before and within the animation? – Lyndsey Scott Oct 25 '14 at 13:45
  • strange behavior, but how do you populate the second table: Is it on the same view controller? Is it pre populated? – carlodurso Oct 25 '14 at 14:06
  • Lyndsey if I remove the 2 "layoutIfNeeded" before the animation block then it makes no difference to the animation. If I remove the ones inside the animation blocks then the animation doesn't happen. The changes just snap into place. – pls Oct 25 '14 at 14:57
  • Carlodurso I have added the code where I populate the tableviews. Yes they on on the same view controller. – pls Oct 25 '14 at 15:01

1 Answers1

0

I tried to duplicate your problem, but got slightly different results. Instead of a sideways movement, I got a downward movement of the label within the cell (but only the first time I did the animation).

I noticed that if I set the table view's height constraint's constant value to 0 in viewDidLoad, cellForRowAtIndexPath was not called until I did the expansion, so I'm guessing that some of the laying out of views of the table view wasn't happening until then.

I fixed the problem by setting the alpha value of the table view to 0 in IB, and set its height to 400 (the value I wanted after expansion). Then in viewDidAppear, I did this,

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.heightCon.constant = 0;
    self.tableView.alpha = 1;
}

This caused the table's data source methods to run right away, but with the table view not visible. Now when I animated the expansion, I didn't get any movement of the label within the cell.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thanks for the answer. I implemented the remainder of the functionality and found that each subsequent animation is fine. Was going to post update to question but you beat me to it :-) Have tried your fix and it's better. Now only the top bottom few animate instead of all of them. – pls Oct 26 '14 at 08:34