Search code examples
iosxcoderx-swiftcollectionview

Collectionviewcell images not load before scroll


Hello o created UICollectionView without storyboard and i used RxSwift for data binding.

I showing images on cells but they're not showing before scroll.

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

And my viewcontroller code is

    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)

Solution

  • I guess the problem is here

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

    as the frame of firstView may not yet correctly known , so encourage you to add constraints to the imageView


    also if the imageView has same frame as it's parent ( firstView ) so you may get rid of that firstView and directly add the imageview to

    self.contentView.addSubview(imageView)
    

    Also i see no loading for the remote image so consider using SDWebImage

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