arshu
arshu

Reputation: 11915

Stop UITableView from autoscrolling in iOS 8

I have a tableview that auto scrolls only in iOS 8 when I open a new view using [self.navigationController pushViewController:newViewController animated:YES];

Detailed Problem :

Now I'm going to give a detail of how I'm getting the problem, it's particularly in iOS 8. Take any tableview having around 50 entries, scroll it down to make the 10th entry at the top of the tableview. Then select any item on the tableview. Use the below method to push a view controller on row selection in tableview.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // Open the New View
    NewViewController *newVC = [[NewViewController alloc] init];
    [self.navigationController pushViewController:newVC animated:YES];
}

Now you will find that on coming back from NewViewController to the previous ViewController, the tableview autoscrolls some distance. The tableview doesn't stay at the place of scroll it always changes its position automatically.

Upvotes: 2

Views: 748

Answers (2)

Victor Choy
Victor Choy

Reputation: 4246

Just remove self.tableView.estimatedRowHeight = xxx; It works for me . The problem happens in iOS8 and does not appear in iOS10

Upvotes: 0

sash
sash

Reputation: 8715

I had the same problem. I was using the new iOS 8 feature dynamic cell heigh. I was setting:

self.tableView.estimatedRowHeight = 50.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;

The problem was that some cells were much higher than 50. Solution was to provide delegate method estimatedHeightForRowAtIndexPath and return values that are closer to the actual cell height. For example:

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 1) {
        return 300.0;
    }

    return 50;
}

Another solution is to calculate cell heigh using systemLayoutSizeFittingSize inside cellForRowAtIndexPath and cache that value. Inside estimatedHeightForRowAtIndexPath supply cached values but return default if the cell height wasn't cached yet.

Upvotes: 1

Related Questions