【发布时间】: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是集合视图布局的抽象基类,并不打算实例化。相反,您应该实例化其内置的具体子类之一,例如UICollectionViewFlowLayout或UICollectionViewCompositionalLayout或您自己的UICollectionViewLayout的自定义子类。 -
非常感谢!现在可以了!我犯了多么愚蠢的错误
标签: ios swift uicollectionview uicollectionviewcell