Home > OS >  Nested UIView inside UICollectionViewCell not updating
Nested UIView inside UICollectionViewCell not updating

Time:09-01

I'm trying to display a list of recepcies for at pet proyect, which I've modeled with a UICollectionView and for each item, a custom UICollectionViewCell.

This UICollectionViewCell has inside a UIImageView and a custom UIView for layout pruposes.

The tree goes like this

HomeViewController (UIViewController)
└── UICollectionView
    └── RecepieCardCollectionViewCell (UICollectionViewCell)
        ├── UIImageView
        └── RecepieCardInfoView (UIView)
            └── UILabel

I'm comforming to the UICollectionViewDelegate and UICollectionViewDataSource protocols at the screenshot demonstring the labels not being updated

Things I've tried:

  • Placing .setNeedsDisplay() to all the element, with no results.
  • Using DispatchQueue.main.async but didn't work, which makes sense since I'm not using an API request to show the recepies (for now).
  • Move the label inside RecepieCardInfoView to the RecepieCardCollectionViewCell. This worked for some reason, but I would like to understand why.

If you need more context of the code, you can find the full repository here at the branch feat/recepies-list

I've been asked to add the full code of the cell, here it is:

import UIKit
import Kingfisher

class RecepieCardCollectionViewCell: UICollectionViewCell {
    static let identifier = "RecepieCardUICollectionViewCell"
    
    let thumbnail: UIImageView = {
        let image = UIImageView()
        image.translatesAutoresizingMaskIntoConstraints = false
        image.kf.setImage(with: URL(string: "https://i.imgur.com/ISxVZHA.png"))
        return image
    }()
    
    let content: UIView = {
        let view = UIView(frame: .zero)
        view.clipsToBounds = true
        view.layer.cornerRadius = 40
        return view
    }()
    
    let cardInfoView = RecepieCardInfoView(frame: .zero)

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .themeWhite
        layer.cornerRadius = 40
        
        addSubview(content)
        content.frame = bounds
        content.addSubview(thumbnail)
        
        let cardInfoView = RecepieCardInfoView(frame: .zero)
        
        content.addSubview(cardInfoView)

        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.17
        layer.shadowOffset = .zero
        layer.shadowRadius = 10
        
        NSLayoutConstraint.activate([
            thumbnail.leadingAnchor.constraint(equalTo: content.leadingAnchor),
            thumbnail.trailingAnchor.constraint(equalTo: content.trailingAnchor),
            thumbnail.topAnchor.constraint(equalTo: content.topAnchor),
            thumbnail.heightAnchor.constraint(equalTo: thumbnail.widthAnchor),
            cardInfoView.topAnchor.constraint(equalTo: content.bottomAnchor, constant: -100),
            cardInfoView.bottomAnchor.constraint(equalTo: content.bottomAnchor),
            cardInfoView.leadingAnchor.constraint(equalTo: content.leadingAnchor),
            cardInfoView.trailingAnchor.constraint(equalTo: content.trailingAnchor),
        ])
    }
    
    required init(coder: NSCoder) {
        fatalError()
    }
    
    func configure(_ recepie: Recepie) {
        cardInfoView.configure(recepie)
        thumbnail.kf.setImage(with: URL(string: recepie.getCover()))
    }
}

Thanks you in advance!

CodePudding user response:

You create 2 instances of cardInfoView inside RecepieCardCollectionViewCell

let cardInfoView = RecepieCardInfoView(frame: .zero) // here 1

override init(frame: CGRect) {
    super.init(frame: frame)
    
    backgroundColor = .themeWhite
    layer.cornerRadius = 40
    
    addSubview(content)
    content.frame = bounds
    content.addSubview(thumbnail)
    
    let cardInfoView = RecepieCardInfoView(frame: .zero) // and here 2

The problem is that you add the inner view without updating it's content , and update the outer view without adding it to cell hierarchy

  • Related