如果 CollectionView 只需要一张图片
自定义的 UICollectionView 一般都会去注册自定义的 Cell 来完成特殊需求,但部分时间我们只需要一张图片即可,不需要自定义 Cell ,那么可以这么做:
// 初始化方法中写下
register(UICollectionViewCell.self, forCellWithReuseIdentifier: "identifier")
在对应的代理方法中:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath)
let cellImage = UIImage(named: "imageName")
cell.addSubview(cellImage)
return cell
}