未显示集合视图,从未调用 cellForItemAtIndexPath
Collection View isn't being displayed, cellForItemAtIndexPath is never called
我有一个 UITableView
,自定义单元格包含 UIStackView
。作为准备新单元格的一部分,当 Table 视图通过其 cellForRowAtIndexPath
方法添加新单元格时,它会实例化并添加任何集合视图(MultaCollectionView
类型)和任何 UILabel
需要添加到单元格的堆栈视图(单元格可能包含各种集合视图)。理论上,堆栈视图包含一系列标签和集合视图。
尽管标签显示正确,但集合视图在运行时未显示。我尝试显示的集合视图在 .xib
Interface Builder 文档中定义。
正在调用集合视图的 numberOfSectionsInCollectionView
方法,但从未调用 collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
方法。
为什么 collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
方法从未被调用?为什么集合视图不在堆栈视图中呈现?
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
通过以下方法添加单元格:
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 与任何其他大小一样。尝试给 collectionView 明确的宽度和高度限制,看看是否有帮助。
我的假设是,由于这是在 UITableView 的单元格内发生的,所以每当 table 视图单元格放回 "reuse pool" 时,集合视图的委托就会断开连接或被禁用.这将取决于为 table 视图提供单元格的代码(我们在代码示例中没有看到)。
其他地方也可能有一些代码从集合视图中清除数据源。 (不太可能但值得检查)
问题已通过在我的 UICollectionView
subclass 中覆盖 UIView
的 intrinsicContentSize()
方法解决,如下所示(查看 this question):
override func intrinsicContentSize() -> CGSize {
return self.collectionViewLayout.collectionViewContentSize()
}
顺便说一句,我还忘记为 Cell 重用标识符注册一个 class。所以我修改了初始化器:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
self.dataSource = self
self.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
我终于让它工作了。从故事板视图控制器转换到非故事板集合视图控制器时,您必须在推送到控制器时执行此操作:
func showChatLogController(user: User) {
let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
chatLogController.user = user
chatLogController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(chatLogController, animated: true)
}
更具体地说,您必须使用 collectionViewLayout: UICollectionViewFlowLayout
我有一个 UITableView
,自定义单元格包含 UIStackView
。作为准备新单元格的一部分,当 Table 视图通过其 cellForRowAtIndexPath
方法添加新单元格时,它会实例化并添加任何集合视图(MultaCollectionView
类型)和任何 UILabel
需要添加到单元格的堆栈视图(单元格可能包含各种集合视图)。理论上,堆栈视图包含一系列标签和集合视图。
尽管标签显示正确,但集合视图在运行时未显示。我尝试显示的集合视图在 .xib
Interface Builder 文档中定义。
正在调用集合视图的 numberOfSectionsInCollectionView
方法,但从未调用 collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
方法。
为什么 collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
方法从未被调用?为什么集合视图不在堆栈视图中呈现?
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
通过以下方法添加单元格:
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 与任何其他大小一样。尝试给 collectionView 明确的宽度和高度限制,看看是否有帮助。
我的假设是,由于这是在 UITableView 的单元格内发生的,所以每当 table 视图单元格放回 "reuse pool" 时,集合视图的委托就会断开连接或被禁用.这将取决于为 table 视图提供单元格的代码(我们在代码示例中没有看到)。
其他地方也可能有一些代码从集合视图中清除数据源。 (不太可能但值得检查)
问题已通过在我的 UICollectionView
subclass 中覆盖 UIView
的 intrinsicContentSize()
方法解决,如下所示(查看 this question):
override func intrinsicContentSize() -> CGSize {
return self.collectionViewLayout.collectionViewContentSize()
}
顺便说一句,我还忘记为 Cell 重用标识符注册一个 class。所以我修改了初始化器:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
self.dataSource = self
self.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
我终于让它工作了。从故事板视图控制器转换到非故事板集合视图控制器时,您必须在推送到控制器时执行此操作:
func showChatLogController(user: User) {
let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
chatLogController.user = user
chatLogController.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(chatLogController, animated: true)
}
更具体地说,您必须使用 collectionViewLayout: UICollectionViewFlowLayout