0

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

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
}

任何帮助都非常感谢。

4

1 回答 1

0

@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

于 2019-03-19T12:27:42.763 回答