On the iPad I show a UIPopover
when the user selects cells in a UITableView
. The cell stays selected until the popover is dismissed.
When the user rotates the device from portrait to landscape orientation and the selected cell was on the lower part of the screen, it will disappear after the rotation and the popover ends up pointing at another (indifferent) cell.
How can I make sure that the selected cell in a UITableView
stays on screen when rotating from portrait to landscape orientation?
Update: Combining Caleb's and kviksilver's codes, the following is a working solution:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
CGRect activeCellRect = [self.tableView rectForRowAtIndexPath:self.indexPath];
if ((activeCellRect.origin.y + activeCellRect.size.height) >
(self.view.frame.origin.y + self.view.frame.size.height))
{
// If a row ends up off screen after a rotation, bring it back
// on screen.
[self.tableView scrollToRowAtIndexPath:self.indexPath
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];
}
}
Update 2, on repositioning the UIPopover
: After the scroll command it is necessary to send a reloadData
message to the table view. Then the rectForRowAtIndexPath:
method will correctly report the new position of the cell (otherwise it will not, as it is not updated properly after the scroll-command)!