重复使用的自定义CollectionViewCell保留旧的大小属性

问题描述 投票:0回答:2

问题

我有UICollectionView,它实现了方法sizeForItemAt。我希望单元格具有sizeForItemAt方法中分配的height属性,但是当单元格被重用时,size属性仍然存在。

所以我想知道是否有任何方法可以迫使可重复使用的单元格更改其高度?

截图

这里是链接:https://i.stack.imgur.com/33Fyf.png

代码

cellForItemAt:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> 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
  }

sizeForItemAt:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let answerCellHeightMultiplier: CGFloat = answer.count > 18 ? 0.28 : 0.37
    return CGSize(width: (self.frame.width / cellCount) - cellSpacing, height: self.frame.height * answerCellHeightMultiplier)
}

自定义UICollectionViewCell:

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
  }()
}
ios swift uicollectionview custom-cell
2个回答
0
投票

UICollectionViewDelegateFlowLayout具有函数collectionView(_:layout:sizeForItemAt:),该函数为集合视图的每个可见单元格调用。您的实现返回一个固定宽度和一个动态高度,值self.frame.height * answerCellHeightMultiplier,其中answerCellHeightMultiplieranswer.count > 18 ? 0.28 : 0.37,具体取决于属性answer。因此,如果您的单元格应具有取决于属性answer的动态高度,则在显示收集视图期间必须更改answer,这是不可能的。我的建议是在sizeForItemAt中使用answer而不是在每个单元格中都具有正确的高度值的数组。


0
投票

我已经意识到实际的问题是约束,当在sizeForItemAt方法中更改单元格的大小时,约束没有更新。

帮助我的是updateConstraints()函数,该函数已在我的自定义视图单元格中覆盖:

override func updateConstraints() {
    super.updateConstraints()

    let resizedLetterButton = Helpers.resizeImageSize(uiView: self, ratio: 42/48, multiplier: 1)

    answerLetterButton.updateConstraint(attribute: .width, constant: resizedLetterButton.width)
    answerLetterButton.updateConstraint(attribute: .height, constant: resizedLetterButton.height)
  }

然后我在cellForItemAt中称呼它:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: answerLetterCell, for: indexPath) as! AnswerLetterCell

    cell.updateConstraints()
    ...
}

这解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.