在iPad上,当用户选择
当用户将设备从纵向旋转为横向时,如果所选单元格位于屏幕下部,则旋转后该单元格将消失,而弹出窗口则指向另一个(无关紧要的)单元格。
如何确保在从纵向旋转为横向时,
更新:结合Caleb和kviksilver的代码,以下是可行的解决方案:
UITableView
中的单元格时,我会显示一个UIPopover
。在弹出窗口关闭之前,单元格会保持选中状态。当用户将设备从纵向旋转为横向时,如果所选单元格位于屏幕下部,则旋转后该单元格将消失,而弹出窗口则指向另一个(无关紧要的)单元格。
如何确保在从纵向旋转为横向时,
UITableView
中的选定单元格仍然保持在屏幕上?更新:结合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];
}
}
更新2,关于重新定位 UIPopover
: 在滚动命令之后需要向表格视图发送一个 reloadData
消息。然后 rectForRowAtIndexPath:
方法将正确报告单元格的新位置(否则它将不会,因为它在滚动命令后没有正确更新)!
[self.tableView indexPathsForVisibleRows]
在旋转后不会返回行数组的“缩小”大小。不过,Caleb建议使用-rectForRowAtIndexPath:
方法确实有效。 - user8472