UICollectionViewCell重用导致不正确的UISwitch状态

时间:2019-01-26 07:00:52

标签: ios swift xcode uicollectionviewcell uicollectionviewdelegate

我无法找到解决此问题的方法。 我在UISwitch内使用UICollectionViewCell,并且传递了一个布尔变量来设置打开或关闭。

条件是所有单元一次只能打开一个开关。 但是当我打开一个开关时,另一个随机开关的颜色会改变,这意味着其状态已改变。

默认情况下,情节提要中的开关状态为ON,即使我将其设置为OFF,也不会发生任何变化。

我不知道为什么会这样。

这是我的cellForItemAtIndexPath

代码
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: AddEditItemPopupView.cellId, for: indexPath) as! DiscountCollectionViewCell
        cell.delegate = self

        let currentDiscount = allDiscounts[indexPath.item]
        let shouldApplyDiscount = updatedDiscountId == currentDiscount.id
        cell.updateCellWith(data: currentDiscount, applyDiscount: shouldApplyDiscount)
        return cell
    }

以及单元格类的代码

func updateCellWith(data: DiscountModel, applyDiscount: Bool) {
        let name = data.title.replacingOccurrences(of: "Discount ", with: "")
        self.titleLabel.text = String(format: "%@ (%.2f%%)", name, data.value)
        self.switchApply.isOn = applyDiscount
        self.switchApply.tag = data.id
    }

数据源包含DiscountModel的对象,如下所示:

{
    id: Int!
    title: String!
    value: Double!
}

更改单元格类内部的值的方法:

@IBAction func switchValueChanged(_ sender: UISwitch) {
        if sender.isOn {
            self.delegate?.switchValueDidChangeAt(index: sender.tag)
        }
        else{
            self.delegate?.switchValueDidChangeAt(index: 0)
        }
    }

视图控制器类内部的委托方法:

func switchValueDidChangeAt(index: Int) {
        self.updatedDiscountId = index
        self.discountCollectionView.reloadData()
    }

1 个答案:

答案 0 :(得分:2)

我建议对您的代码进行一些改进;

  • 重新加载整个收藏集视图有点a弹枪
  • 由于可能没有折扣,您应该为所选折扣使用可选选项,而不是“ 0”
  • 使用Tag通常是有问题的

我会使用类似的东西:

var currentDiscount: DiscountModel? = nil

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: AddEditItemPopupView.cellId, for: indexPath) as! DiscountCollectionViewCell
    cell.delegate = self

    let item = allDiscounts[indexPath.item]
    self.configure(cell, forItem: item)

    return cell
}

func configure(_ cell: DiscountCollectionViewCell, forItem item: DiscountModel) {
    cell.switchApply.isOn = false
    let name = item.title.replacingOccurrences(of: "Discount ", with: "")
    self.titleLabel.text = String(format: "%@ (%.2f%%)", name, item.value)

    guard let selectedDiscount = self.currentDiscount else {
        return
    }

    cell.switchApply.isOn = selectedDiscount.id == item.id
}

func switchValueDidChangeIn(cell: DiscountCollectionViewCell, to value: Bool) {
    if value {
        if let indexPath = collectionView.indexPath(for: cell) {
           self.currentDiscount = self.allDiscounts[indexPath.item]
        }
    } else {
        self.currentDiscount = nil
    }
    for indexPath in collectionView.indexPathsForVisibleItems {
        if let cell = collectionView.cellForItem(at: indexPath) {
            self.configure(cell, forItem: self.allDiscounts[indexPath.item])
        }
    }
}

在您的单元格中

@IBAction func switchValueChanged(_ sender: UISwitch) {
    self.delegate?.switchValueDidChangeIn(cell:self, to: sender.isOn)
}