[英]In Swift, how to see the same cells for a table view when an orientation happens?
I have a table view whose cells and cell contents are arranged by auto layout. 我有一个表格视图,其单元格和单元格内容通过自动布局进行排列。 I also change cell height when orientation happens by returning different values in this function: 我还通过在此函数中返回不同的值来更改定向发生时的像元高度:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (UIApplication.sharedApplication().statusBarOrientation.isLandscape.boolValue == true) {
// cell heights in landscape
if (...) {
return 100
} else
return 80
}
// cell heights in portrait
if (...) {
return 220
} else if (... ) {
return 240
} else {
return 250
}
}
When an orientation happens, everything works fine except that the visible cells are not the same. 当定向发生时,除了可见的单元格不同之外,其他所有东西都可以正常工作。 For example, let's say I have 30 cells in total. 例如,假设我总共有30个单元格。 When I scroll the table in Portrait mode and I see cells 8, 9, 10, 11. And when I rotate the device to Landscape mode, I see cells 20, 21, 22, 23, 24. 在“纵向”模式下滚动表格时,看到单元格8、9、10、11。在将设备旋转到“横向”模式时,我看到单元格20、21、22、23、24。
I actually hope to see 8, 9, 10, 11 right after I rotate to Landscape mode in this case, which is the friendly behavior considering UI design. 在这种情况下,我实际上希望在旋转到“风景”模式后立即看到8、9、10、11,这是考虑到UI设计的友好行为。
The root cause is because the height of the cells are changed when the orientation happens. 根本原因是因为定向发生时单元格的高度发生了变化。 Is there any way I can adjust the table view so that I can see the same cells when the device orientation changes? 有什么方法可以调整表格视图,以便在设备方向更改时可以看到相同的单元格?
You could always do something like this: 您总是可以这样做:
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
yourTableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: false)
}
To get the current visible top cell NSIndexPath you can do this: 要获取当前可见的顶部单元格NSIndexPath,可以执行以下操作:
yourTableView.indexPathsForVisibleRows![0]
indexPathsForVisibleRows
returns an array of the NSIndexPaths of the visible cells in the UITableView and the first one at index 0 is the top one. indexPathsForVisibleRows
返回UITableView中可见单元格的NSIndexPaths数组,索引0处的第一个是顶部的数组。
override func willRotateToInterfaceOrientation (toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval
{
// Save the visible row position
visibleRows = tableview.indexPathsForVisibleRows;
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation)
{
// Scroll to the saved position prior to screen rotate
}
For iOS 10/Xcode8/Swift 3 add the following to UITableView: 对于iOS 10 / Xcode8 / Swift 3,将以下内容添加到UITableView:
// retain same position in UITableView on screen rotation
override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
let indexPath = tableView.indexPathsForVisibleRows![0]
tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: false)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.