gpt4 book ai didi

ios - 拆分 UICollectionView & UICollectionViewDataSource 显示黑屏

转载 作者:行者123 更新时间:2023-11-28 15:25:48 25 4
gpt4 key购买 nike

我正在开始一个包含 Collection View 的新项目,该项目将从 REST api 下载大量数据。我已将 Collection View 和数据源拆分为两个文件,但是当我运行该应用程序时,我得到的只是黑屏。我看到了一些问题并尝试更改背景,将 Collection View 添加为 subview ,但似乎没有任何效果。我没有遇到任何错误,调试 View 层次结构和 View (从后到前)列为 UIWindoW -> MainSearchVC -> UICollectionView。

我原本以为没有单元格被填充,但它们应该是因为我在单元格中设置了 UIImage。我不确定在哪里可以找到这个。我的代码在下面 - 如果有人有从它的数据源中拆分 Collection View 的经验或者为什么事情不起作用,请帮助:)

class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainSearchVC()

return true
}
}

class MainSearchVC: UICollectionViewController {
init(){
super.init(collectionViewLayout: UICollectionViewFlowLayout())
self.collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: UICollectionViewFlowLayout())
self.collectionView?.dataSource = MainSearchDataSource()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
collectionView?.register(ImageCell.self, forCellWithReuseIdentifier: "imagecell")
}

override func viewDidLoad() {
super.viewDidLoad()
self.collectionView?.backgroundColor = UIColor.clear
self.collectionView?.tintColor = UIColor.blue
NSLog("Visible Cells: " + String(describing: self.collectionView?.visibleCells.count))
self.view.addSubview(self.collectionView!)
}
}

class MainSearchDataSource: NSObject, UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1;
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imagecell", for: indexPath)
cell.backgroundColor = UIColor.red
return cell
}
}

class ImageCell: UICollectionViewCell {

override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

let imageView: UIImageView = {
let iv = UIImageView()
iv.image = UIImage(contentsOfFile: "SR71")
iv.contentMode = .scaleAspectFill
return iv
}()

func setupViews(){
addSubview(imageView)

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

}

最佳答案

MainSearchVC Collection View dataSource 委托(delegate)有问题

首先你应该理解Zeroing Weak Reference

dataSource 委托(delegate)维护对MainSearchDataSource对象 引用。而你的陈述是

self.collectionView?.dataSource = MainSearchDataSource()

刚刚分配分配。 它不会保留数据源对象

您必须创建一个类变量 并分配MainSearchDataSource 对象。它将保留对象引用,直到 MainSearchVC 从内存中释放。

class MainSearchVC:  UICollectionViewController {

var searchDataSource: MainSearchDataSource?

init(){

super.init(collectionViewLayout: UICollectionViewFlowLayout())
self.collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: UICollectionViewFlowLayout())

searchDataSource = MainSearchDataSource()
self.collectionView?.dataSource = searchDataSource

}

// Remaining code of your `MainSearchVC`
}

关于ios - 拆分 UICollectionView & UICollectionViewDataSource 显示黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45267535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com