我有一个(水平)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
}