未显示集合视图,从不调用cellForItemAtIndexPath [英] Collection View isn't being displayed, cellForItemAtIndexPath is never called

查看:86
本文介绍了未显示集合视图,从不调用cellForItemAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableView with custom cells containing UIStackView. As part of preparing a new cell, when the Table View adds new cells via its cellForRowAtIndexPath method, it instantiates and adds any collection views (of type MultaCollectionView) and any UILabel which need to be added to the cell’s Stack View (a cell may include various collection views). In theory, a stack view contains a sequence of Labels and Collection Views.

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

Although labels are displaying correctly, the Collection View is not being displayed at runtime. The Collection View I’m attempting to display is defined in a .xib Interface Builder document.

集合视图的 numberOfSectionsInCollectionView 方法被调用,但是从不调用 collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)方法。

The Collection View’s numberOfSectionsInCollectionView method is getting called, however the collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) method is never called.

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

Why is the collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) method never called? Why aren’t Collection Views being rendered in the Stack 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 通过以下方法添加Cell:

The parent TableView adds Cells via the following method:

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的可见区域之外。

Since you are getting the call back for the numberOfSectionsInCollectionView this would suggest the datasource is connected up correctly but if a cell is not going to be visible based on the current layout then the collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) will not get called until it is needed. The reason a cell might not be visible could be because your sectionInsets for the collectionView are set large enough that the first cell would be positioned outside the visible area of the collectionView.

另一个原因可能是collectionView的框架太小而无法显示任何单元格。
如果您的集合视图大小为{0,0},您应该看到 numberOfSectionsInCollectionView ,但不会接到<$的调用c $ c> cellForItemAtIndexPath
因为单元格不可见。

Another reason could be that the collectionView's frame is too small to display any cells. If your collectionview as a size of {0,0} you should get the calls as you are seeing to the numberOfSectionsInCollectionView but will not get the calls to cellForItemAtIndexPath since the cells will not be visible.

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

Perhaps try ensuring that your collectionView has a frame size that is large enough to display the cells you expect to see. You may be seeing the UILabels correctly because they have an implicit intrinsicContentSize which ensures they display well in a stackView whereas the CollectionView is just as happy to have a size of 0,0 as it would be with any other size. Try giving the collectionView explicit width and height constraints to see if that helps.

这篇关于未显示集合视图,从不调用cellForItemAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆