Giuseppe Iacobucci
Giuseppe Iacobucci

Reputation: 423

UITableView Scroll automatically to the top after begin/end updates call for adjust height

I have a TableViewController that initialize my cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FIDPostTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell for this indexPath
    [cell updateFonts];
    [cell loadDataWithPost:[self.posts objectAtIndex:indexPath.item]];
    cell.parentTableViewController = self;
    cell.indexPath = indexPath;
    [cell draw];
    if(self.selectedIndex == indexPath.row){
        //Do expand cell stuff
    } else{
        //DO closed cell stuff
    }

    return cell;
}

and that responds to heightForRowAtIndexPath:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
  NSString *reuseIdentifier = CellIdentifier;
    FIDPostTableViewCell *cell = [self.offscreenCells objectForKey:reuseIdentifier];
    if (!cell) {
        cell = [[FIDPostTableViewCell alloc] init];
        [self.offscreenCells setObject:cell forKey:reuseIdentifier];
    }

    // Configure the cell for this indexPath
    [cell updateFonts];
    [cell loadDataForHeightCalculationWithPost:[self.posts objectAtIndex:indexPath.item]];
    [cell draw];

         if(self.selectedIndex == indexPath.row){
    return [cell calculateHeight] + 100;
} else{
    return [cell calculateHeight];
}


    }

self.selectedIndex is a int local variable of TableViewController

Each custom cell have inside a button, that respond to a selector when touched, this is my CustomViewCell code:

  self.expandSocialAction = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        self.expandSocialAction.backgroundColor = [FIDUIHelper fideniaLightBlue];
        [self.expandSocialAction addTarget:self action:@selector(selectRow:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.expandSocialAction];

and then:

-(void)selectRow:(id)sender{
    if(self.parentTableViewController.selectedIndex == self.indexPath.row){
        self.parentTableViewController.selectedIndex = -1;
    } else{
    self.parentTableViewController.selectedIndex = self.indexPath.row;
    }
    [self.parentTableViewController.tableView beginUpdates];
    [self.parentTableViewController.tableView endUpdates];

}

The cell have a pointer to parent tableViewContorller: self.parentTableViewController. All work fine, after beginUpdate and endUpdates call, the method heightForRowAtIndexPath is called ( i put a break point in ) and also che cell have the right height.

If i click the button on the first or second row the cell animate and change height fine, but if i scroll down the table and for example i click on the 6th row, the height change but the tableView scroll automatically to the first or second element.

Any suggestion?

Regards,

Upvotes: 4

Views: 1831

Answers (2)

greg
greg

Reputation: 4953

I have a feeling that the heightForRowAtIndexPath: method is part of the problem. Instead of performing the calculation, try to just use two constants in that method: one for expanded, one for collapsed. I'm wondering if the method is taking too long to execute and then thinks the height of the rows are 0, then the calculation finally returns, but the UITableView scroll position is not updated.

Another suggestion might be that your estimatedHeight and heightFor methods are returning two vastly different values.

Upvotes: 4

Geet
Geet

Reputation: 2437

check if you have used ScrollTORowAtIndexPAth someWhere in your code

Upvotes: 0

Related Questions