0

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

我的 tableViewCell 行有一个标签和 collectionView

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

在此处输入图像描述

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

场景:我点击按钮,它改变了它的颜色(在collectionCell中)并突出显示tableViewCell的行,我有10多个tablerows。当我向下滚动 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
    }
4

2 回答 2

0

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

检查此链接

tableView 和 collectionView 几乎相同

于 2020-09-22T16:34:51.007 回答
0

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

slotButton.layer.borderColor = UIColor.black.cgColor

slotButton.backgroundColor = UIColor.clear

于 2020-09-22T18:05:10.267 回答