所以,UICollectionView
现在对我来说真的很痛苦。考虑我有UIViewController
一个UICollectionView
嵌入其中的。好吧,CollectionView 的每个单元格几乎都是UIViewController
. 每个单元格都包含一些按钮和图像。当我选择一个按钮并倾向于使按钮保持其状态时,CollectionView 会重用该单元格,并且还会在其他单元格中复制单元格状态。但是,当我尝试将单元格放入数组中并且想要检查该数组中单元格的状态时,该cellForItemAt
方法会覆盖这些单元格。我感到很困惑。请帮忙。即使prepareForReuse
在UICollectionViewCell
没有帮助。这是一些代码:
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
}
任何帮助都非常感谢。