我有一个UITableView自定义单元格包含UIStackView.作为准备新单元格的一部分,当表视图通过其cellForRowAtIndexPath方法添加新单元格时,它会实例化并添加任何集合视图(类型MultaCollectionView)以及任何UILabel需要添加到单元格的堆栈视图中的单元格(单元格可能包含各种集合视图) ).理论上,堆栈视图包含一系列标签和集合视图.
虽然标签显示正确,但是在运行时不显示集合视图.我试图显示的集合视图在.xibInterface Builder文档中定义.
numberOfSectionsInCollectionView调用Collection View的方法,但是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)
        }                        
    }
}
Dal*_*son 18
由于您正在接收回调,因此numberOfSectionsInCollectionView建议数据源正确连接,但如果根据当前布局不能看到单元格,那么在collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)需要之前不会调用.单元格可能不可见的原因可能是因为collectionView的sectionInsets设置得足够大,以至于第一个单元格将位于collectionView的可见区域之外.
另一个原因可能是collectionView的框架太小而无法显示任何单元格.如果您的集合视图的大小为{0,0},您应该接收到的电话,numberOfSectionsInCollectionView但不会接到电话,cellForItemAtIndexPath因为单元格将不可见.
也许尝试确保您的collectionView的框架大小足以显示您希望看到的单元格.你可能正确地看到了UILabel,因为它们有一个隐含的intrinsicContentSize,可以确保它们在stackView中很好地显示,而CollectionView也可以像任何其他大小那样大小为0,0.尝试给出collectionView显式的宽度和高度约束,看看是否有帮助.
| 归档时间: | 
 | 
| 查看次数: | 7201 次 | 
| 最近记录: |