如果 CollectionView 只需要一张图片 0 个改进

自定义的 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
    }
本文为 Wiki 文章,邀您参与纠错、纰漏和优化
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!