ios - 重用自定义 Collection View 单元格保留旧的大小属性

标签 ios swift uicollectionview custom-cell

问题

我有UICollectionView,它实现了sizeForItemAt方法。我希望单元格具有在 sizeForItemAt 方法中分配的高度属性,但是当重复使用单元格时,大小属性仍然存在。

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

屏幕截图

这是链接:/image/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
  }

项目尺寸:

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

最佳答案

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

关于ios - 重用自定义 Collection View 单元格保留旧的大小属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239811/

相关文章:

ios - 在Ios中 react 原生Google登录问题

ios - Swift UITextField 小数字段类型 - 不粘贴、仅 1 个小数点和长度限制

swift - firebase .indexOn 警告与 autoId

ios - Collection View 单元格内容未显示

iphone - UICollectionView selectItemAtIndexPath 的动画持续时间 :animated:scrollPosition:

ios - 单击 UICollectionViewCell 时如何转换到新 Controller (以编程方式)

ios - 存折应用程序未在 iOS 模拟器中更新?

ios - iOS 应用程序中的复选框

iOS 15 - Xcode 13-RC 警告 : -[NSKeyedUnarchiver validateAllowedClass:forKey:]

arrays - 如何从 Array 创建一个方法来生成一个元组 - Swift