title: Reused Custom CollectionViewCells keep old size properties
body: <h1>The problem</h1>
  
  <p>I have <code>UICollectionView</code>, which implements method <code>sizeForItemAt</code>. I want cells to have the height property which was assigned in <code>sizeForItemAt</code> method, but when the cells are reused the size property persists. </p>
  
  <p>So I wonder if there is any way to force reusable cells to change their height?</p>
  
  <h2>Screenshot</h2>
  
  <p>Here is the link: <a href="https://i.stack.imgur.com/33Fyf.png" rel="nofollow noreferrer">https://i.stack.imgur.com/33Fyf.png</a></p>
  
  <h2>Code</h2>
  
  <p>cellForItemAt:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: answerLetterCell, for: indexPath) as! AnswerLetterCell
  
      cell.answerLetterButton.setTitleColor(Colors.answerLetterButtonColor, for: .normal)
      cell.letter = letters[indexPath.item]
  
      return cell
    }
  </code></pre>
  
  <p>sizeForItemAt:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
      let answerCellHeightMultiplier: CGFloat = answer.count &gt; 18 ? 0.28 : 0.37
      return CGSize(width: (self.frame.width / cellCount) - cellSpacing, height: self.frame.height * answerCellHeightMultiplier)
  }
  </code></pre>
  
  <p>Custom UICollectionViewCell:</p>
  
  <pre><code>class AnswerLetterCell: UICollectionViewCell {
  
    var letter: String? {
      didSet {
        guard let letter = letter else { return }
  
        if letter != " " {
          answerLetterButton.setTitle(letter, for: .normal)
          answerLetterButton.isHidden = false
        } else {
          answerLetterButton.isHidden = true
        }
      }
    }
  
    override init(frame: CGRect) {
      super.init(frame: frame)
      setup()
    }
  
    func setup() {
      addSubview(answerLetterButton)
  
      let resizedLetterButton = Helpers.resizeImageSize(uiView: self, ratio: 42/48, multiplier: 1)
  
      /* Letter Button Constraints */
      answerLetterButton.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
      answerLetterButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
      answerLetterButton.widthAnchor.constraint(equalToConstant: resizedLetterButton.width).isActive = true
      answerLetterButton.heightAnchor.constraint(equalToConstant: resizedLetterButton.height).isActive = true
    }
  
    required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }
  
    /* Views */
  
    let answerLetterButton: UIButton = {
      let uiButton = UIButton()
      uiButton.translatesAutoresizingMaskIntoConstraints = false
      uiButton.setBackgroundImage(#imageLiteral(resourceName: "answer_letter_base.pdf"), for: .normal)
      uiButton.titleLabel?.font = Fonts.letterButtonFont
      uiButton.isUserInteractionEnabled = false
      return uiButton
    }()
  }
  </code></pre>