Невозможно установить imageView в CollectionViewCell - PullRequest
1 голос
/ 07 апреля 2020

В моем коде я пытаюсь установить imageView для UICollectionViewCell. ImageView называется cellImageView в классе CollectionViewCell. Когда приложение загружается, оно пытается инициализировать это значение, но оно не работает

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectCell", for: indexPath) as! PhraseSelectionCell
    cell.cellImageView?.image = UIImage(named: "lol")
    return cell
}

Класс UICollectionViewCell

class PhraseSelectionCell: UICollectionViewCell {
@IBOutlet var cellImageView: UIImageView!

}

1 Ответ

1 голос
/ 07 апреля 2020

попробуйте отладить таким образом, тогда вы сможете найти, где находится ошибка.

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

//debug 1
if UIImage(named: "lol") == nil {
    print("image is nil, double check image name")
}

//debug 2
if cell.cellImageView == nil {
    print("cellImageView outlet is nil")
}

cell.cellImageView?.image = UIImage(named: "lol")
return cell

}

...