滚动前未加载 Collectionviewcell 图像

Collectionviewcell images not load before scroll

你好 o 创建了没有故事板的 UICollectionView,我使用 RxSwift 进行数据绑定。

我在单元格上显示图像,但在滚动之前它们没有显示。

   func first(_ image : String) {

    self.addSubview(firstView)
    firstView.snp.makeConstraints { (make) in
        make.right.equalToSuperview()
        make.left.equalToSuperview()
        make.top.equalToSuperview()
        make.height.equalTo(3 * self.bounds.height / 4)
    }
    firstView.layer.masksToBounds = true
    imageView.image = UIImage(named: "kitaplar")
    imageView.frame = CGRect(x: 0, y: 0, width: firstView.frame.width, height: firstView.frame.height)
    firstView.addSubview(imageView)
}

我的 viewcontroller 密码是

    self.kategoriArray
        .throttle(1, scheduler: MainScheduler.instance)
        .debug("arraItem", trimOutput: false)
        .bind(to: kategoriColl.rx.items(cellIdentifier: "kategoriCell", cellType: KategoriCollectionViewCell.self)){(index,model,cell) in

            cell.first("https://www.gravatar.com/avatar/d2bba3700fd3a3e8cc12888edd980525?s=23&d=identicon&r=PG")
                cell.second(model.title as! String)

        }.disposed(by: disposeBag)

估计问题出在这里

imageView.frame = CGRect(x: 0, y: 0, width: firstView.frame.width, height: firstView.frame.height) 

由于 firstView 的框架可能尚未正确知道,因此鼓励您向 imageView

添加约束

此外,如果 imageView 与其父级 ( firstView ) 具有相同的框架,那么您可以摆脱 firstView 并直接将 imageview 添加到

self.contentView.addSubview(imageView)

我还发现没有加载远程图像,所以考虑使用 SDWebImage

imageView.sd_setImage(with: URL(string:image), placeholderImage: UIImage(named: "kitaplar"))