所以,UICollectionView
现在对我来说真的很痛苦。假设我有一个 UIViewController
,其中嵌入了一个 UICollectionView
。 CollectionView 的每个单元格几乎都是 UIViewController 的整个宽度。每个单元格都包含一些按钮和图像。当我选择一个按钮并倾向于使该按钮保留其状态时,CollectionView 会重用该单元格,并且也会在其他单元格之间复制单元格状态。但是,当我尝试将单元格放入数组中并且想要检查该数组中单元格的状态时,cellForItemAt
方法会覆盖这些单元格。我感到很困惑。请帮忙。即使 UICollectionViewCell
TRONG 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)
} khác {
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
}
Bất kỳ trợ giúp được đánh giá rất cao.
@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)
} khác {
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
Tôi là một lập trình viên xuất sắc, rất giỏi!