未显示集合视图,从不调用cellForItemAtIndexPath

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

虽然标签显示正确,但在运行时不显示集合视图。 我试图显示的集合视图是在.xib Interface Builder文档中定义的。

调用Collection View的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通过以下方法添加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的框架太小而无法显示任何单元格。 如果您的集合cellForItemAtIndexPath大小为{0,0},您应该看到numberOfSectionsInCollectionView的调用,但不会调用cellForItemAtIndexPath因为单元格将不可见。

也许尝试确保您的collectionView的框架大小足以显示您希望看到的单元格。 你可能正确地看到了UILabel,因为它们有一个隐含的intrinsicContentSize ,可以确保它们在stackView中很好地显示,而CollectionView也可以像任何其他大小那样大小为0,0。 尝试给出collectionView显式宽度和高度约束,看看是否有帮助。

我的假设是,由于这发生在UITableView的单元格内部,每当表格视图单元格被放回“重用池”时,集合视图的委托就会断开连接或禁用。 这将取决于为表视图提供单元格的代码(我们在代码示例中没有看到)。

其他地方也可能有一些代码从集合视图中清除数据源。 (不太可能,但值得检查)

通过在我的UICollectionView子类中重写UIViewintrinsicContentSize()方法解决了这个问题 ,如下所示(查看这个问题 ):

 override func intrinsicContentSize() -> CGSize { return self.collectionViewLayout.collectionViewContentSize() } 

另外,我还忘记为Cell重用标识符注册一个类。 所以我修改了初始化程序:

 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