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

时间:2016-01-18 03:56:02

标签: ios swift xcode7

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

虽然标签显示正确,但在运行时不会显示“集合视图”。我试图显示的集合视图是在.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通过以下方法添加单元格:

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)
        }                        
    }

}

4 个答案:

答案 0 :(得分:16)

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

另一个原因可能是collectionView的框架太小而无法显示任何单元格。 如果您的收藏视图的大小为{0,0},您应该看到numberOfSectionsInCollectionView,但不会接到cellForItemAtIndexPath的来电,因为单元格将无法显示。

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

答案 1 :(得分:3)

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

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

答案 2 :(得分:1)

我终于开始工作了。从故事板视图控制器转换到非故事板集合视图控制器时,必须在推送到控制器时执行此操作:

func showChatLogController(user: User) {
    let chatLogController = ChatLogController(collectionViewLayout: UICollectionViewFlowLayout())
    chatLogController.user = user
    chatLogController.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(chatLogController, animated: true)

}

更具体地说,你必须使用collectionViewLayout:UICollectionViewFlowLayout

答案 3 :(得分:0)

通过在UIView子类中覆盖intrinsicContentSize() UICollectionView方法解决了该问题,如下所示(查看this question):

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)
}
相关问题