![](/img/trans.png)
[英]Swift 2.1 - How to pass index path row of collectionView cell to segue
[英]Collectionview cell for row at index path not getting called for subsequent sections if the current section cell height is zero
我在我們項目的集合視圖中遇到了一個問題 -
Collectionview 由 4 個部分組成。 每個部分由 1 個單元格組成。
如果我們將第 0 節中的集合視圖單元格的高度保持為零,則不會為第 0 節調用 cellforrrowatindexpath。 我們面臨的問題是 cellforrowatindexapath 也不會被后續部分調用。 盡管提供了其他部分單元格高度,但集合視圖不會在屏幕上顯示任何內容。
更新:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
Switch indexpath.section {
case 0: return CGSize(width: UIScreen.main.bounds.width, height: 0)
case 1: return CGSize(width: UIScreen.main.bounds.width, height: 200)
case 2: return CGSize(width: UIScreen.main.bounds.width, height: 300)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
Switch indexpath.section {
case 0: return A()
case 1: return B()
case 2: return C()
}
}
如果有人對集合視圖以這種方式工作的原因有更好的理解,請告訴我。
如果您使用 storyboard,請嘗試在 Size Inspector 部分將 Collection View 的Estimate Size
更改為None 。
請檢查:
我曾經遇到過類似的問題。 它可以通過返回 CGFloat.leastNonzeroMagnitude 或 0.1 而不是 0 來解決。
在viewDidLoad()
中添加:
if let fl = theCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
fl.estimatedItemSize = CGSize(width: 1, height: 1)
}
現在sizeForItemAt
和cellForItemAt
將為您的其他部分調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.