gpt4 book ai didi

ios - 未显示 Collection View ,从未调用 cellForItemAtIndexPath

转载 作者:搜寻专家 更新时间:2023-10-30 22:21:36 24 4
gpt4 key购买 nike

我有一个 UITableView,其中的自定义单元格包含 UIStackView。作为准备新单元格的一部分,当 TableView 通过其 cellForRowAtIndexPath 方法添加新单元格时,它会实例化并添加任何 Collection View (类型为 MultiCollectionView)和任何 UILabel 需要添加到单元格的堆栈 View (单元格可能包含各种 Collection View )。理论上,堆栈 View 包含一系列标签和 Collection View 。

虽然标签显示正确,但 Collection View 并没有在运行时显示。我尝试显示的 Collection View 在 .xib Interface Builder 文档中定义。

Collection View 的 numberOfSectionsInCollectionView 方法被调用,但是 collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) 方法从未被调用。

为什么从未调用 collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) 方法?为什么 Collection View 不在堆栈 View 中呈现?

import UIKit
private let reuseIdentifier = "Cell"

class MultaCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
self.dataSource = self
}
class func instanceFromNib() -> UICollectionView {
return UINib(nibName: "multas", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UICollectionView
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
return cell
}

}

父级 TableView 通过以下方法添加 Cells:

func addSubviewsToCellStack(cell: ArticuloTableViewCell, texto: [[String: String]]) {\            
if isLabel == true {
let label = UILabel()
label.text = "blah"
subview = label
cell.textoStack.addArrangedSubview(subview)
} else {
let colview = MultaCollectionView.instanceFromNib()
cell.textoStack.addArrangedSubview(colview)
}
}

}

最佳答案

由于您收到了 numberOfSectionsInCollectionView 的回调,这表明数据源已正确连接,但如果根据当前布局,单元格将不可见,则 collectionView (collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) 在需要时才会被调用。单元格可能不可见的原因可能是因为您的 collectionView 的 sectionInsets 设置得足够大,以至于第一个单元格将位于 collectionView 的可见区域之外。

另一个原因可能是 collectionView 的框架太小而无法显示任何单元格。如果您的 collectionview 的大小为 {0,0},您应该会收到对 numberOfSectionsInCollectionView 的调用,但不会收到对 cellForItemAtIndexPath 的调用,因为单元格将不可见。

也许尝试确保您的 collectionView 的框架大小足以显示您希望看到的单元格。您可能会正确地看到 UILabels,因为它们有一个隐式的 intrinsicContentSize 确保它们在 stackView 中显示良好,而 CollectionView 的大小为 0,0 与任何其他 View 一样高兴尺寸。尝试为 collectionView 提供明确的宽度和高度限制,看看是否有帮助。

关于ios - 未显示 Collection View ,从未调用 cellForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com