【问题标题】:sizeForItemAtIndexPath and cellForItemAtIndexPath is not called未调用 sizeForItemAtIndexPath 和 cellForItemAtIndexPath
【发布时间】:2017-11-13 05:57:42
【问题描述】:

我尝试在 UICollectionViewCellCell 内创建 UICollectionView,但未调用 func sizeForItemAtIndexPath 和 cellForItemAtIndexPath,这是我的代码:

import UIKit

class HistoryCollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource{
    static let cellId = "cell"

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpCollectionView()

    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    let appsCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
        return collectionView
    }()
    func setUpCollectionView(){
        backgroundColor = UIColor.whiteColor()
        addSubview(appsCollectionView)
        appsCollectionView.delegate = self
        appsCollectionView.dataSource = self
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        print("sizeForItem is called")
        return CGSizeMake(100, 100)
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        print("cellForItem is called")
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        return cell
    }
}

希望有人帮忙

【问题讨论】:

    标签: ios swift swift2.3


    【解决方案1】:

    更新答案:

    你忘了注册第二个collectionview要使用的单元格:

    appsCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell2")
    

    其次,为视图控制器设置一个有效的框架:

    let frame = CGRect(x: 8, y: 8, width: 300, height: 200)
    let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
    

    您可能在这里混淆了几个概念。为什么您的单元格实现 UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource ?如果您还不关心干净的架构而只是想测试一下,请让您的 CollectionView 的容器视图控制器实现上述协议(可以是通用 VC 或 UICollectionViewController 之类的子类)。

    您调用的 HistoryCollectionViewCell 应该是 HistoryCollectionViewController 和子类 UIViewController 或 UICollectionViewController 而不是 UICollectionViewCell。

    这些是单独的职责。您可能想查看 Github 以获取示例或 Apple 的 UIKit 文档(例如:https://developer.apple.com/documentation/uikit/uicollectionviewdelegate)。或者网上一些教程:https://www.raywenderlich.com/136159/uicollectionview-tutorial-getting-started

    class HistoryCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
        // TODO: Refactor
    
        // Implement required methods
    }
    
    
    class HistoryCollectionViewCell: UICollectionViewCell {
        // Implement required methods
    }
    

    【讨论】:

    • 因为我想在 UICollectionViewCell 中添加一个 UICollectionView,这样我就可以在另一个 UICollectionView 中注册它。我跟着这个:stackoverflow.com/questions/17729582/…
    • 我的错。将 collectionView = UICollectionView(frame: .zero,...) 更改为有效的框架
    • 不要相信我的话,但我会说如果宽度和高度都为零,CollectionView 不会重新加载它的数据。因此不会调用数据源函数。
    猜你喜欢
    • 1970-01-01
    • 2016-12-30
    • 2015-08-28
    • 2014-08-17
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多