拆分UICollectionView& UICollectionViewDataSource显示黑屏

时间:2017-07-23 16:44:50

标签: ios swift uicollectionview

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

我原本以为没有任何细胞被填充,但它们应该是因为我在细胞中设置了UIImage。我不确定在哪里寻找这个。我的代码如下 - 如果有人有从分割数据源中分割集合视图的经验或为什么事情不起作用,请帮助:)

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)
    }

}

1 个答案:

答案 0 :(得分:1)

MainSearchVC集合视图dataSource代理

出现问题

首先,您应该了解 Zeroing Weak Reference

dataSource代表正在对weak Object MainSearchDataSource进行self.collectionView?.dataSource = MainSearchDataSource()个引用。你的陈述是

class variable

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

您必须创建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` } 从内存中释放。

popup.html