iPad:在屏幕上保留选定的表格视图单元格

时间:2023-01-01 22:19:38

On the iPad I show a UIPopover when the user selects cells in a UITableView. The cell stays selected until the popover is dismissed.

在iPad上,当用户在UITableView中选择单元格时,我会显示UIPopover。单元格保持选中状态,直到弹出窗口被解除。

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?

当从纵向旋转到横向时,如何确保UITableView中的选定单元格保留在屏幕上?

Update: Combining Caleb's and kviksilver's codes, the following is a working solution:

更新:结合Caleb和kviksilver的代码,以下是一个有效的解决方案:

-(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)!

更新2,重新定位UIPopover:在scroll命令之后,需要将reloadData消息发送到表视图。然后rectForRowAtIndexPath:方法将正确报告单元格的新位置(否则它将不会,因为它在scroll-command之后没有正确更新)!

2 个解决方案

#1


4  

On orientation change try checking indexPathsForVisibleRows to see if your cell is visible and then using scrollToRowAtIndexPath if not.. something like:

在方向更改时尝试检查indexPathsForVisibleRows以查看您的单元格是否可见,然后使用scrollToRowAtIndexPath(如果不是)...类似于:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    if (![[self.tableView indexPathsForVisibleRows] containsObject:[self.tableView indexPathForSelectedRow]]) {
        [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForSelectedRow] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

#2


2  

You already know which row is selected, right? You also know when the device orientation changes, or at least you can know that, because there are UIViewController methods for that. You can get the rectangle for the selected row using UITableView's -rectForRowAtIndexPath: method, and it's easy to make sure that rectangle stays visible using UIScrollView's -scrollRectToVisible:animated: method, which UITableView inherits.

你已经知道选择了哪一行,对吧?您还知道设备方向何时更改,或者至少您可以知道,因为有UIViewController方法。您可以使用UITableView的-rectForRowAtIndexPath:方法获取所选行的矩形,并且可以使用UIScrollView的-scrollRectToVisible:animated:方法(UITableView继承)确保矩形保持可见。

#1


4  

On orientation change try checking indexPathsForVisibleRows to see if your cell is visible and then using scrollToRowAtIndexPath if not.. something like:

在方向更改时尝试检查indexPathsForVisibleRows以查看您的单元格是否可见,然后使用scrollToRowAtIndexPath(如果不是)...类似于:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    if (![[self.tableView indexPathsForVisibleRows] containsObject:[self.tableView indexPathForSelectedRow]]) {
        [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForSelectedRow] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

#2


2  

You already know which row is selected, right? You also know when the device orientation changes, or at least you can know that, because there are UIViewController methods for that. You can get the rectangle for the selected row using UITableView's -rectForRowAtIndexPath: method, and it's easy to make sure that rectangle stays visible using UIScrollView's -scrollRectToVisible:animated: method, which UITableView inherits.

你已经知道选择了哪一行,对吧?您还知道设备方向何时更改,或者至少您可以知道,因为有UIViewController方法。您可以使用UITableView的-rectForRowAtIndexPath:方法获取所选行的矩形,并且可以使用UIScrollView的-scrollRectToVisible:animated:方法(UITableView继承)确保矩形保持可见。