Search code examples
objective-cuitableviewuiscrollviewuiscrollviewdelegate

UIScrollView selected cell goes out of present view on rotation to landscape


I have a UITableView in a UIScrollView. The tables cells are vertical, i.e. one below the other. If I select the bottom cell, in portrait, it highlights and displays a popover. However, on rotating to landscape, the selected cell goes out of the visible view, but its popover remains. I understand that UIScrollView on rotate uses the top most cell from the UITableView as the topmost in the new (Landscape) orientation. How do I force it to still keep the selected cell in the view on rotation, i.e. on rotation, possibly rotate with the selected cell as the point of reference.


Solution

  • I believe you need to call this method in your interface rotation event of view controller.

    -(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath
                 atScrollPosition:(UITableViewScrollPosition)scrollPosition
                         animated:(BOOL)animated;
    

    Here is what you can do in your view controller:

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromOrientation
    {
        [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
    

    You can get the indexPath of selected row from the following event:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath