Stefan Kendall
Stefan Kendall

Reputation: 67802

Scroll tableview cell directly beneath the navigation bar?

I have a tableview. On tap, a cell expands into a form, and I would like the cell to scroll up to start right below the navigation bar. How do I achieve this?

Here's the starting state:
Starting state

Currently, after tap:
Current behavior

And here's what I want:
Desired behavior

Upvotes: 1

Views: 190

Answers (2)

jrturton
jrturton

Reputation: 119242

scrollToRowAtIndexPath... or scrollToNearestSelectedRow... both take "scroll position" arguments which allow the selection to go to the top, middle or bottom of the table view. That should give you what you want.

Upvotes: 1

Vitalii Gozhenko
Vitalii Gozhenko

Reputation: 9354

You need something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Perform cell change height code here
    // ...

    // After that scroll to this cell
    CGFloat sectionHeaderHeight = [self tableView:tableView heightForHeaderInSection:indexPath.section];
    CGRect cellFrame = [tableView rectForRowAtIndexPath:indexPath];
    [tableView setContentOffset:CGPointMake(0, cellFrame.origin.y - sectionHeaderHeight) animated:YES];
}

Upvotes: 2

Related Questions