我想显示一个在按下按钮后随动画一起显示的collectionView。
collectionView正确显示,但其中的collectionViewCells不正确。
我已经正确设置了UICollectionViewDelegate和UICollectionViewDataSource方法,但未调用它们(已通过breakPoints测试)。
我正在做所有的事情([以编程方式,所以我还必须将[UI]通用UICollectionViewCell的SettingCell
注册到相应的cellID。
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
}
}
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 }()