我很难找到解决这个问题的办法。
我正在使用
UISwitch
UICollectionViewCell
我传递了一个布尔变量来设置开关。
条件是所有电池一次只需打开一个开关。
但当我打开一个开关,另一个随机开关的颜色改变,这意味着它的状态改变。
默认情况下,在情节提要中开关状态为“开”,即使我将其设置为“关”,也不会发生任何更改。
我不明白为什么会这样。
这是我的密码
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()
}