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
}
单元格显示为蓝色,因此我知道collectionView正在运行。 nibName是正确的,重用标识符有效。调试时说cell.image是零。我尝试了很多不同的组合,我不确定如何继续。感谢
答案 0 :(得分:0)
将BrowseCell的文件所有者更改为托管BrowseCell
的ViewController
答案 1 :(得分:0)
问题是在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
}