Свойства xib Swift iOS CollectionView равны нулю - PullRequest
0 голосов
/ 02 мая 2018

BrowseCell.swift

import UIKit

class BrowseCell: UICollectionViewCell {

    @IBOutlet weak var image: UIImageView!

    @IBOutlet weak var overlayView: UIImageView!

}

CustomCollectionViewController.swift

override func viewDidLoad() {
    // collectionView?.register(BrowseCell.self, forCellWithReuseIdentifier: "browse")
    collectionView?.register(UINib(nibName: "BrowseCell", bundle: nil), forCellWithReuseIdentifier: "browse")

    collectionView?.dataSource = self
    collectionView?.delegate = self
}

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

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "browse", for: indexPath) as! BrowseCell
    cell.image = UIImageView(image: UIImage(named: "6"))
    cell.overlayView = UIImageView(image: UIImage(named: "6"))
    let test = UIImageView(image: UIImage(named: "6"))
    cell.backgroundColor = UIColor.blue
    return cell
}

BrowseCell.xib

Ячейки отображаются синим цветом, поэтому я знаю, что collectionView работает. NibName правильный, идентификатор повторного использования работает. Во время отладки он говорит, что cell.image - ноль. Я перепробовал много разных комбинаций и не уверен, как продолжить. Спасибо

Ответы [ 2 ]

0 голосов
/ 03 мая 2018

проблема присваивается в imageView.image

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "browse", for: indexPath) as! BrowseCell

        /// Here you can try making a change 
        /// You are assigning imageView.image a new ImageView which Is wrong 
        cell.image = UIImageView(image: UIImage(named: "6"))

        /// Replace above line as 
        cell.image.image = UIImage.init(named: "6")

        cell.overlayView = UIImageView(image: UIImage(named: "6"))
        let test = UIImageView(image: UIImage(named: "6"))
        cell.backgroundColor = UIColor.blue
        return cell
    }
0 голосов
/ 03 мая 2018

Измените владельца файла BrowseCell на ViewController, в котором находится BrowseCell

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...