【问题标题】:CollectionViewCells don't appear programmatically - Swift 5CollectionViewCells 不会以编程方式出现 - Swift 5
【发布时间】:2020-04-24 21:47:14
【问题描述】:

我想在点击按钮后显示一个带有动画的 collectionView。

collectionView 显示正确,但里面的 collectionViewCells 不正确。

我已经正确设置了 UICollectionViewDelegate 和 UICollectionViewDatasource 方法,但是它们没有被调用(用断点测试)。

我正在以编程方式做所有事情,所以我还必须将 SettingCell 注册到相应的 cellID 中,这是一个常见的 UICollectionViewCell。

我还认为可能存在问题,因为我设置委托和数据源为时已晚,但我不这么认为,因为它们是在母类的 init 开始时设置的。

class SettingsLauncer : NSObject, UICollectionViewDelegate, UICollectionViewDataSource{

var collectionView : UICollectionView = {
    let layout = UICollectionViewLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = .white
    return cv
}()


let CelliD = "CelliD"


func handleMoreButton(){
            //creating and adding  View
    if let windowView = UIApplication.shared.keyWindow {

        let width = UIScreen.main.bounds.width
        let height = UIScreen.main.bounds.height
        let heightCollectionView = CGFloat(300)
        let myHeight = height - heightCollectionView

        collectionView.frame = CGRect(x: 0, y: height, width: width, height: 0)

        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {

            windowView.addSubview(self.collectionView)

            self.collectionView.frame = CGRect(x: 0, y: myHeight, width: width, height: heightCollectionView)
        }, completion: nil)
    }
}



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CelliD, for: indexPath)

    return cell
}



override init(){
    super.init()
    collectionView.register(SettingCell.self, forCellWithReuseIdentifier: CelliD)
    collectionView.delegate = self
    collectionView.delegate = self

}
}

【问题讨论】:

  • UICollectionViewLayout 是集合视图布局的抽象基类,并不打算实例化。相反,您应该实例化其内置的具体子类之一,例如 UICollectionViewFlowLayoutUICollectionViewCompositionalLayout 或您自己的 UICollectionViewLayout 的自定义子类。
  • 非常感谢!现在可以了!我犯了多么愚蠢的错误

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

请在您的 init 方法中添加这两行

    self.collectionView.delegate = self
    self.collectionView.dataSource = yourDataSource

目前你有

    collectionView.delegate = self
    collectionView.delegate = self

还要让你的 collectionView 变得懒惰......并在那里分配委托

private lazy var collectionView : UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = .white
    cv.delegate = self
    cv.dataSource = yourDataSource
    cv.translatesAutoresizingMaskIntoConstraints = false
    return cv
}()

【讨论】:

  • 我尝试了你的建议,但它不起作用,还没有调用委托和数据源方法
  • 请现在试一试......如果它解决了您的问题,请告诉我
  • 按照你的建议做了,但没有,委托方法还没有被调用,我在 numberOfItemsInSection 和 cellForItemAt 设置了 2 个断点,但它们都没有被调用。
  • 你的单元格数据源是什么?
  • 添加到view后尝试重新加载youf collectionView的数据...
猜你喜欢
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
相关资源
最近更新 更多