UICollectionView - 为什么不调用这些方法?
UICollectionView - Why are the methods not called?
我从另一个集合视图单元格调出一个新的集合视图:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let rezeptLauncher = RezeptLauncher()
rezeptLauncher.ShowRezept()
}
之后它显示了集合视图,但里面没有单元格。
我几乎阅读了关于这个主题的每一页。什么都不起作用。有什么建议么?非常感谢您!
class RezeptLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width / 2.5, height: collectionView.frame.width / 2)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = .red
return cell
}
var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.backgroundColor = .white
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
return cv
}()
func ShowRezept() {
print("Rezept wird angezeigt")
if let keyWindow = UIApplication.shared.keyWindow {
keyWindow.addSubview(collectionView)
collectionView.backgroundColor = .white
collectionView.topAnchor.constraint(equalTo: keyWindow.topAnchor, constant: 40).isActive = true
collectionView.leadingAnchor.constraint(equalTo: keyWindow.leadingAnchor, constant: 40).isActive = true
collectionView.trailingAnchor.constraint(equalTo: keyWindow.trailingAnchor, constant: -40).isActive = true
collectionView.bottomAnchor.constraint(equalTo: keyWindow.bottomAnchor, constant: -40).isActive = true
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
}
}
}
您的 Rezept 对象似乎正在被 Swift 释放,因为它的生命周期在您的 did select 函数中。
如果它被释放,collectionView 就没有委托了。由于根据最佳实践,委托是弱实体,因此它们不会被强烈持有(因此解除分配将其删除)
您应该将其添加为包含外部 collectionView 的 class 的实例,并且只调用 didSelect 中的设置函数。
我从另一个集合视图单元格调出一个新的集合视图:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let rezeptLauncher = RezeptLauncher()
rezeptLauncher.ShowRezept()
}
之后它显示了集合视图,但里面没有单元格。
我几乎阅读了关于这个主题的每一页。什么都不起作用。有什么建议么?非常感谢您!
class RezeptLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width / 2.5, height: collectionView.frame.width / 2)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = .red
return cell
}
var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.backgroundColor = .white
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
return cv
}()
func ShowRezept() {
print("Rezept wird angezeigt")
if let keyWindow = UIApplication.shared.keyWindow {
keyWindow.addSubview(collectionView)
collectionView.backgroundColor = .white
collectionView.topAnchor.constraint(equalTo: keyWindow.topAnchor, constant: 40).isActive = true
collectionView.leadingAnchor.constraint(equalTo: keyWindow.leadingAnchor, constant: 40).isActive = true
collectionView.trailingAnchor.constraint(equalTo: keyWindow.trailingAnchor, constant: -40).isActive = true
collectionView.bottomAnchor.constraint(equalTo: keyWindow.bottomAnchor, constant: -40).isActive = true
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
}
}
}
您的 Rezept 对象似乎正在被 Swift 释放,因为它的生命周期在您的 did select 函数中。
如果它被释放,collectionView 就没有委托了。由于根据最佳实践,委托是弱实体,因此它们不会被强烈持有(因此解除分配将其删除)
您应该将其添加为包含外部 collectionView 的 class 的实例,并且只调用 didSelect 中的设置函数。