CollectionViewCells 不会以编程方式出现 - Swift 5
CollectionViewCells don't appear programmatically - Swift 5
我想显示一个 collectionView,它在按下按钮后随动画一起出现。
collectionView 显示正确,但里面的 collectionViewCells 显示不正确。
我已经正确设置了 UICollectionViewDelegate 和 UICollectionViewDatasource 方法,但是没有调用它们(使用断点测试)。
我正在以编程方式做所有事情所以我还必须注册 SettingCell
这是一个通用的 UICollectionViewCell 到各自的 cellID。
我还认为可能会有问题,因为我设置委托和数据源的时间太晚了,但我不这么认为,因为它们是在母亲 class 的初始化中设置的。
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
}()
我想显示一个 collectionView,它在按下按钮后随动画一起出现。
collectionView 显示正确,但里面的 collectionViewCells 显示不正确。
我已经正确设置了 UICollectionViewDelegate 和 UICollectionViewDatasource 方法,但是没有调用它们(使用断点测试)。
我正在以编程方式做所有事情所以我还必须注册 SettingCell
这是一个通用的 UICollectionViewCell 到各自的 cellID。
我还认为可能会有问题,因为我设置委托和数据源的时间太晚了,但我不这么认为,因为它们是在母亲 class 的初始化中设置的。
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
}()