UICollectionView |电池可重复使用

UICollectionView | Cell reusable

所以,UICollectionView 现在对我来说真的很痛苦。假设我有一个 UIViewController,其中嵌入了一个 UICollectionView。 CollectionView 的每个单元格几乎都是 UIViewController 的整个宽度。每个单元格都包含一些按钮和图像。当我 select 一个按钮并倾向于使按钮保持其状态时,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