UICollectionViewController не регистрирует созданный пользовательский класс ячейки - PullRequest
0 голосов
/ 30 мая 2018

Я пытаюсь изменить цвет клетки.У меня есть файл с именем HomeController, где я размещаю ячейку.Я создал пользовательскую ячейку с именем HomePostCell.Я зарегистрировал HomePostCell в HomeController и ничего не произошло.Цвета ячеек не меняются.У меня есть код ниже для обоих файлов.

Контроллер:

import UIKit

class HomeController: UICollectionViewController {
    let cellId = "cellId"

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = .white
        collectionView?.register(HomePostCell.self, forCellWithReuseIdentifier: cellId)
    }

    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: cellId, for: indexPath) as! HomePostCell

        return cell
    }
}

Ячейка:

import UIKit

class HomePostCell: UICollectionViewCell {
    let photoImageView: UIImageView = {
        let iv = UIImageView()
        iv.backgroundColor = .blue
        return iv
    }()

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

        addSubview(photoImageView)
        photoImageView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
    }

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

Я хочу, чтобы появилось пять синих ячеек, но это не так.

1 Ответ

0 голосов
/ 30 мая 2018
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomePostCell
cell.setupData()

return cell
}
...