ios - 调用 invalidateLayout() 或 reloadData() 以在设备旋转后重新加载从情节提要创建的 UICollectionView
问题描述
在UITableViewController
, 中UITableViewCell
,我有一个网格,UICollectionViewCells
每行具有固定数量的静态(无间距)。
extension NounsTVC: UICollectionViewDataSource {
func collectionView( _ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellIds.count
}
func collectionView( _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell( withReuseIdentifier: cellIds[indexPath.item], for: indexPath)
cell.layer.borderColor = UIColor.gray.cgColor
cell.layer.borderWidth = 0.5
return cell
}
}
extension NounsTVC: UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let availableWidth = collectionView.bounds.size.width
let minimumWidth = floor(availableWidth / cellsPerRow)
let remainder = availableWidth - minimumWidth * CGFloat(cellsPerRow)
let columnIndex = indexPath.row % Int(cellsPerRow)
let cellWidth = CGFloat(columnIndex) < remainder ? minimumWidth + 1 : minimumWidth
return CGSize(width: cellWidth, height: 45)
}
}
工作正常。
但是,当我旋转设备时,它需要调用这些布局方法。
这看起来像答案:Swift: How to refresh UICollectionView layout after rotation of the device
但我没有我UICollectionView
的 asinvalidLayout()
因为它UICollectionView
是在 IB 中创建的(据我所知),那么我如何在我的 IB 上调用 invalidateLayoutUICollectionView
呢?
解决方案
在上述情况中发现了两个问题。
首先,xcode 没有通过在情节提要和 .swift 文件之间拖动一条线来在我的自定义 UITableViewCell 中创建 IBOutlet(显然是某些星座中的错误)。解决方案是手动输入代码,然后按 ctrl 键将其反向拖放到情节提要中的正确视图。
@IBOutlet weak var myCollectionView: UICollectionView!
其次,因为集合视图是在自定义 UITableViewCell 中实例化的,所以无法使用 viewWillTransitionToSize:withTransitionCoordinator: 或 viewWillLayoutSubviews(表视图方法)。但是, UITableViewCell 确实具有以下内容,其中可以调用 .invalidateLayout() :
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
{
super.traitCollectionDidChange(previousTraitCollection)
guard previousTraitCollection != nil else
{ return }
myCollectionView.collectionViewLayout.invalidateLayout()
}
推荐阅读
- c - C 编程:将长整数传递给函数并返回一个字符串
- c++ - 链接库错误 c/缺少 dll/错误版本?
- javascript - 将大型模型列表从视图发送到控制器 c# mvc razor
- python - 将 Python 列表列表转换为 MATLAB 稀疏矩阵
- python - 如何根据使用 tkinter 和 python 的 quixo 游戏在按钮网格上的每个按钮上添加文本
- python - 从给定的整数列表创建直方图
- xcode - 如果@EnvironmentObject,如何创建通用?
- powershell - 如何将 $input 自动变量转换为列表(在结束块中)?
- python - 使用 Python、BeautifulSoup 和 Requests 传递链接
- php - 如果 ip 不同,Wordpress 重定向到登录页面