我的应用程序有一个 UIViewController
类;在这个类中,我连接了一个从 Storyboard 加载的 UICollectionView
。
我正在使用 UICollectionViewLayout
类创建自定义布局。这是它的样子:
class MyLayout: UICollectionViewLayout {
override func prepareLayout() {
super.prepareLayout()
}
override func collectionViewContentSize() -> CGSize {
let attributes = super.collectionViewContentSize()
return attributes
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
let attributes = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes]
return attributes
}
override func layoutAttributesForItemAtIndexPath(indexPath:
NSIndexPath) -> UICollectionViewLayoutAttributes {
let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
return attributes
}
}
要将 UICollectionViewLayout
分配给 UICollectionView
,我使用 UICollectionView
的 collectionViewLayout
属性:
myCollectionView.collectionViewLayout = MyLayout()
运行应用程序,UICollectionViewCells
不再可见。不过,它们在分配 UICollectionViewLayout
之前是可见的。我现在只能看到 UICollectionView
的背景。
为什么细胞不再可见?
更新
我仔细查看了我的 UICollectionView
的 UICollectionViewLayoutAttributes
,尤其是 contentSize
。我打印出它的值,它似乎等于 (0.0, 0.0)
。 layoutAttributesForElementsInRect
的 attributes
值也等于 nil
。绝对是一个危险信号。
Download the project
最佳答案
我认为您几乎可以保持所有内容完全相同。将自定义布局的类类型更改为 UICollectionViewFlowLayout
。
class myLayout: UICollectionViewFlowLayout {
//all your code here
}
此外,将 myLayout 更改为 MyLayout 以备不时之需:)
关于ios - 为什么在将 UICollectionViewLayout 分配给 UICollectionView 后,UICollectionViewCells 不再可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30078582/