【发布时间】:2019-09-09 15:26:09
【问题描述】:
在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 没有invalidLayout(),因为UICollectionView 是在IB 中创建的(据我所知),那么如何在我的UICollectionView 上调用invalidateLayout?
【问题讨论】:
-
CollectionView为什么不用invalidLayout()?同时更改设备方向
-
您是否可以从
UITableViewCell访问UICollectionView?如果是,请覆盖UITableViewCell中的layoutSubviews并在UICollectionView上调用invalidateLayout。 -
@surToTheW - 在情节提要中创建静态单元格,我不确定如何实例化或访问 UITableViewCell? @ Nikunj - 你能链接到完整的细节吗?会试试的。
-
我错过了关于静态单元格的部分。您可以在 TableViewController 中为它们提供插座。或者更好的出口系列。它们有多少个细胞?然后你可以覆盖
viewWillTransitionToSize:withTransitionCoordinator:或viewWillLayoutSubviews并在那里调用invalidateLayout。 -
@surToTheW - 是的,确实,这非常完美。谢谢你。我在 IB 中建立连接时遇到了麻烦(它不会通过 ctrl-drag 和 drop 来实现),但是根据您的说明,我将其编码并反向连接,现在它可以工作了。再次感谢。
标签: ios swift xcode uicollectionview storyboard