Ios iPad:在屏幕上保留选定的表格视图单元格
在iPad上,当用户在Ios iPad:在屏幕上保留选定的表格视图单元格,ios,ipad,uitableview,Ios,Ipad,Uitableview,在iPad上,当用户在UITableView中选择单元格时,我会显示一个uipover。该单元格将保持选中状态,直到弹出框被取消 当用户将设备从纵向旋转到横向,并且选定的单元格位于屏幕的下部时,旋转后该单元格将消失,popover最终指向另一个(不相关的)单元格 如何确保从纵向旋转到横向时,UITableView中的选定单元格保持在屏幕上 更新:结合Caleb和kviksilver的代码,以下是一个可行的解决方案: -(void)didRotateFromInterfaceOrientation
UITableView
中选择单元格时,我会显示一个uipover
。该单元格将保持选中状态,直到弹出框被取消
当用户将设备从纵向旋转到横向,并且选定的单元格位于屏幕的下部时,旋转后该单元格将消失,popover最终指向另一个(不相关的)单元格
如何确保从纵向旋转到横向时,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
消息。然后rectforrowatinexpath:
方法将正确报告单元格的新位置(否则它将不会报告,因为它在滚动命令后未正确更新) 更改方向时,请尝试检查indexPathsForVisibleRows以查看单元格是否可见,如果不可见,请使用ScrollToRowatineXpath。。比如:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if (![[self.tableView indexPathsForVisibleRows] containsObject:[self.tableView indexPathForSelectedRow]]) {
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForSelectedRow] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
}
您已经知道选择了哪一行,对吗?您还知道设备方向何时改变,或者至少您可以知道,因为有UIViewController方法可用于此。您可以使用UITableView的
-rectforrowatinexpath:
方法获取所选行的矩形,并且使用UITableView继承的UIScrollView的-scrollRectToVisible:animated:
方法可以很容易地确保矩形保持可见。接下来是kviksilver的方法,还有一个-scrollToNearestSelectedRowAtScrollPosition:animated:
,听起来它完成了我在一个步骤中描述的大部分内容。我完全忘记了scrollToNearestSelectedRowAtScrollPosition:-D@kviksilver,我完全忘记了-scrollToRowAtIndexPath…
,直到你提到它。团队合作.-)感谢您的代码,但仍然存在一个问题:[self.tableView indexPathsForVisibleRows]
在旋转后不返回行数组的“缩减”大小。不过,Caleb建议使用-rectforrowatinexpath:
方法确实有效。