gpt4 book ai didi

ios - UICollectionView | UICollectionView |电池可重复使用

转载 作者:行者123 更新时间:2023-11-29 05:55:13 26 4
gpt4 key购买 nike

所以,UICollectionView 现在对我来说真的很痛苦。假设我有一个 UIViewController ,其中嵌入了一个 UICollectionView 。 CollectionView 的每个单元格几乎都是 UIViewController 的整个宽度。每个单元格都包含一些按钮和图像。当我选择一个按钮并倾向于使该按钮保留其状态时,CollectionView 会重用该单元格,并且也会在其他单元格之间复制单元格状态。但是,当我尝试将单元格放入数组中并且想要检查该数组中单元格的状态时,cellForItemAt 方法会覆盖这些单元格。我感到很困惑。请帮忙。即使 UICollectionViewCell 中的 prepareForReuse 也没有帮助。这是一些代码:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! AddressCollectionViewCell
cell.shopAddressDetailLbl.text = ""
cell.addressObj = addresses[indexPath.row]
cell.configureCellForAddress(cell.addressObj)
cell.cellTag = indexPath.row
cell.cellDelegate = self
if addressCells.contains(cell) == false {
addressCells.append(cell)
} else {
if cell.isAddressConfirmed == true {
cell.confirmAddress.setTitle("CONFIRMED", for: .normal)
cell.confirmAddress.isEnabled = false
cell.confirmAddress.backgroundColor
= UIColor(red: 0, green: 100/255, blue: 0, alpha: 1)
addressCells[indexPath.row] = cell
}
}
return cell
}

extension AddressesCollectionViewController: AddressCollectionViewCellDelegate {
func confirmBtnPressed(confirmAddressObj: Address, cell:AddressCollectionViewCell) {
for cellTemp in addressCells {
if cellTemp == cell && cellTemp.isAddressConfirmed == false {
if let dele = addressCollectionViewDelegate {
cellTemp.isAddressConfirmed = true
dele.configureCellsAccordingToChanges(cell: cellTemp)
}
}
}
}
}

override func prepareForReuse() {
super.prepareForReuse()

cellTag = 0
confirmAddress.setTitle("Confirm Address", for: .normal)
confirmAddress.backgroundColor = APP_UNIVERSAL_COLOR
confirmAddress.isEnabled = true
}

非常感谢任何帮助。

最佳答案

🙌@Vadian,@Abu Ul Hassan 👍

相当光滑!对于其他需要这方面帮助的人。 Vadian 在评论中建议我只需要更新和监控我的模型,这正是我所做的。所以就这样:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! AddressCollectionViewCell
cell.shopAddressDetailLbl.text = ""
cell.addressObj = addresses[indexPath.row]
cell.configureCellForAddress(cell.addressObj)
cell.cellTag = indexPath.row
cell.cellDelegate = self

if addresses[indexPath.row].isConfirmed! == true {
cell.confirmAddress.setTitle("CONFIRMED", for: .normal)
cell.confirmAddress.isEnabled = false
cell.confirmAddress.backgroundColor = UIColor(red: 0, green: 100/255, blue: 0, alpha: 1)
} else {
cell.confirmAddress.setTitle("Confirm Address", for: .normal)
cell.confirmAddress.isEnabled = true
cell.confirmAddress.backgroundColor = APP_UNIVERSAL_COLOR
}
return cell
}

extension AddressesCollectionViewController: AddressCollectionViewCellDelegate {
func confirmBtnPressed(confirmAddressObj: Address, cell:AddressCollectionViewCell) {
if confirmAddressObj.isConfirmed! == false {
if let dele = addressCollectionViewDelegate {
cell.isAddressConfirmed = true
dele.configureCellsAccordingToChanges(cell: cell)
}
}
}
}

它还活着:D

关于ios - UICollectionView | UICollectionView |电池可重复使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55240420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com