ios - 未显示 Collection View ,从未调用 cellForItemAtIndexPath

标签 ios swift xcode7

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

虽然标签显示正确,但 Collection View 并没有在运行时显示。我尝试显示的 Collection View 在 .xib Interface Builder 文档中定义。

Collection View 的 numberOfSectionsInCollectionView 方法被调用,但是 collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) 方法从未被调用。

为什么从未调用 collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) 方法?为什么 Collection View 不在堆栈 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 通过以下方法添加 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 的框架太小而无法显示任何单元格。 如果您的 collectionview 的大小为 {0,0},您应该会收到对 numberOfSectionsInCollectionView 的调用,但不会收到对 cellForItemAtIndexPath 的调用,因为单元格将不可见。

也许尝试确保您的 collectionView 的框架大小足以显示您希望看到的单元格。您可能会正确地看到 UILabels,因为它们有一个隐式的 intrinsicContentSize 确保它们在 stackView 中显示良好,而 CollectionView 的大小为 0,0 与任何其他 View 一样高兴尺寸。尝试为 collectionView 提供明确的宽度和高度限制,看看是否有帮助。

关于ios - 未显示 Collection View ,从未调用 cellForItemAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34847069/

相关文章:

ios - 调用 [self.navigationController pushViewController :animated:] 时需要很长时间将 UIView 推送到 UINavigation Controller

ios - 违反 Core Data 的线程合约EXC_BREAKPOINT(代码=1,子代码=0x1f0ad1c8c)

Swift 在滑动时保留 UIPageViewController 中的文本字段内容

swift - 调暗 super View Controller 但不调暗 subview Controller

ios - 在 Swift 中从整数数组创建 NSIndexSet

swift - 在 Swift 中使用完成处理程序动画图像

ios - registerNib UiTableView 在项目损坏后无法正常工作

ios - 我的简单 map 项目无法在模拟器中获取和显示我的位置

ios - 创建通用的 iOS 框架

ios - 如何为UIView添加一条边框线