在prepareForResue上重置tableView中的collectionviewcell

问题描述

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

我的tableViewCell行具有标签和collectionView

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

enter image description here

点击按钮时,我会突出显示按钮的颜色和tableCell的行,这很正常。我对prepareForReuse()有问题

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

我在tableviewcell中添加了prepareForReuse(),但如何从表的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
    }

解决方法

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

Check this link

tableView和collectionView几乎相同

,

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

slotButton.layer.borderColor = UIColor.black.cgColor

slotButton.backgroundColor = UIColor.clear