почему UIColletionView с ленивым классом ячеек регистра var выдает ошибку? - PullRequest
0 голосов
/ 10 мая 2018

Когда я объявляю представление коллекции с областью действия, оно не регистрирует класс ячейки

lazy var flowLayout:UICollectionViewFlowLayout = {

        let f = UICollectionViewFlowLayout()
        f.scrollDirection = .horizontal
        f.itemSize = CGSize(width: 100, height: 100)
        f.minimumInteritemSpacing = CGFloat.greatestFiniteMagnitude
        f.minimumLineSpacing = 5
        return f
    }()

    lazy var collection: UICollectionView = {

        let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: self.flowLayout)
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.showsVerticalScrollIndicator = false
        cv.showsHorizontalScrollIndicator = false
        cv.dataSource = self
        cv.delegate = self
        cv.register(PreviewFilmsPosterCell.self, forCellWithReuseIdentifier: cellId)
        cv.backgroundColor = .black
        return cv
    }()

Выдает эту ошибку:

Завершение приложения из-за необработанного исключения NSInternalInconsistencyException, причина: попытка зарегистрировать класс ячейки, который не является подклассом UICollectionViewCell

что такое правильное решение?

обновление

но если я заявляю, что так, то это работает

 override func viewDidLoad() {

        collectionview = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionview.translatesAutoresizingMaskIntoConstraints = false
        collectionview.dataSource = self
        collectionview.delegate = self
        collectionview.register(PreviewFilmsPosterCell.self, forCellWithReuseIdentifier: cellId)
        collectionview.showsVerticalScrollIndicator = false
        collectionview.showsHorizontalScrollIndicator = false
        collectionview.backgroundColor = .black

}

my PreviewFilmsPosterCell Class

class PreviewFilmsPosterCell: UICollectionViewCell {

    let profileImageButton: UIButton = {
        let button = UIButton()
        button.layer.cornerRadius = 50
        button.clipsToBounds = true
        button.setImage(UIImage(named: "profile_batman"), for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

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

        addViews()
    }

    func addViews(){

        backgroundColor = UIColor.clear

        addSubview(profileImageButton)

        profileImageButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
        profileImageButton.widthAnchor.constraint(equalToConstant: 100).isActive = true

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

Ответы [ 3 ]

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

Проблема здесь

cv.register(PreviewFilmsPosterCell.self, forCellWithReuseIdentifier: cellId)

этот PreviewFilmsPosterCell класс не является подклассом UICollectionViewCell

он должен быть

class PreviewFilmsPosterCell:UICollectionViewCell
0 голосов
/ 10 мая 2018

Я нашел свою ошибку в collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

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

PreviewFilmsPosterCell является НЕ подклассом UICollectionViewCell.

Он должен наследовать UICollectionViewCell для использования в UICollectionView

Убедитесь, что ваша ячейка наследуется правильно

class PreviewFilmsPosterCell: UICollectionViewCell {...}
...