【问题标题】:Collectionviewcell images not load before scrollCollectionviewcell 图像在滚动前未加载
【发布时间】:2019-01-20 21:10:16
【问题描述】:

你好,我创建了没有故事板的 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)
}

我的视图控制器代码是

    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)

【问题讨论】:

    标签: ios xcode rx-swift collectionview


    【解决方案1】:

    我猜问题出在这里

    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"))
    

    【讨论】:

    • 我使用 Snapkit 作为框架。滚动后图像正确显示。
    • 这就是滚动后框架已知但未加载的原因,也是本地或远程加载的图像?
    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多