图像未正确显示在UICollectionViewCell中

问题描述

我正在尝试将图像添加到一系列UICollectionView单元中,但是遇到一些问题。这是代码

let myCollectionView: UICollectionView = {
    
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let view = UICollectionView(frame: .zero,collectionViewLayout: layout)
    view.translatesAutoresizingMaskIntoConstraints = false
    view.register(UICollectionViewCell.self,forCellWithReuseIdentifier: "cell")
    
    return view
    
} ()

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",for: indexPath)
    cell.backgroundColor = .lightGray
    let imageView = UIImageView()
    imageView.frame = cell.frame
    cell.contentView.addSubview(imageView)
    imageView.image = determineImageForCells(for: collectionView,at: indexPath.item)
    


    return cell
}

运行该应用程序时,收集视图可以很好地加载,但奇怪的是,它只会加载该行中几个单元格的图像,可能就像10个单元格中的3个一样,其余的则显示浅灰色背景。更奇怪的是,如果我在集合视图中滚动一点,一些单元格上会出现更多图像,而其他单元格会消失。

有关如何解决此问题的任何建议?预先感谢!

干杯!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)