【问题标题】:Reset collectionviewcell which is in tableView on prepareForResue在 prepareForResue 上重置 tableView 中的 collectionviewcell
【发布时间】:2021-01-08 19:15:55
【问题描述】:

我有一个(水平的)collectionView,它位于 tableViewCell 中。

我的 tableViewCell 行有一个 label 和 collectionView

Collectionview 在每个单元格中都有按钮

当点击按钮时,我会突出显示按钮的颜色和 tableCell 的行,这是完美的工作。我对 prepareForReuse() 有疑问

场景:我点击了按钮,它改变了它的颜色(在 collectionCell 中)并突出显示 tableViewCell 的行和 我有 10 多个表行。 当我向下滚动 tableView 时,我看到我在 tableRow1 上所做的选择出现在 tableRow10 中

我在 tableviewcell 中添加了 prepareForReuse(),但是如何从 table 的 prepareForReuse() 引用和重置 collectionview 内的按钮外观

请指教

    // This is my tableCell reuse method
    override func prepareForReuse() {
        super.prepareForReuse()
        tableCellLabel.text = nil
        // Reset collectionViewCell button display here
    }

   // This is my collectionView Datasource method:
   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ActivitySlotCollectionViewCell.identifier, for: indexPath) as! ActivitySlotCollectionViewCell
            cell.configure(from: dataModel)
           return cell
       }

  // This is my collectionViewCell reuseMethod
  override func prepareForReuse() {
        super.prepareForReuse()
        isButtonSelected = false
        slotButton.setTitleColor(UIColor.blue, for: .normal)
        slotButton.layer.borderColor = isButtonSelected ? UIColor.white.cgColor : UIColor.black.cgColor
        slotButton.backgroundColor = isButtonSelected ? UIColor.red : UIColor.clear
    }

【问题讨论】:

    标签: ios uitableview uicollectionview uicollectionviewcell prepareforreuse


    【解决方案1】:

    检查以下链接。每次我们出列一个可重复使用的单元格时,我们都需要清理它,实际上是最后一个消失的单元格。出于性能原因,您应该只重置与内容无关的单元格属性,例如 alpha、编辑和选择状态。重用单元格时,tableView(_:cellForRowAt:) 中的表格视图委托应始终重置所有内容。

    Check this link

    tableView 和 collectionView 几乎一样

    【讨论】:

    • 能否请您重新发布链接..评论中的链接无效
    【解决方案2】:

    尝试将其添加到 prepareForReuse() 函数中,因为基本上您没有为正在重用的单元格设置默认设计,这就是为什么您看到在 tableRow1 上所做的选择出现在 tableRow10 中的原因

    slotButton.layer.borderColor = UIColor.black.cgColor

    slotButton.backgroundColor = UIColor.clear

    【讨论】:

    • 嗨 Nathaniel Gonzales,谢谢..这解决了我的 tableView 问题,但我遇到的主要问题是 collectionviewCell 中的按钮外观
    • 您能否详细解释一下该按钮所指的外观是什么?您希望在取消选择后将其重置为默认设计吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多