Ios 您好,我正在使用swift,我正在尝试在选择集合视图单元格时更改集合视图单元格的标签

Ios 您好,我正在使用swift,我正在尝试在选择集合视图单元格时更改集合视图单元格的标签,ios,swift,iphone,xcode,Ios,Swift,Iphone,Xcode,我这里有我的集合视图控制器,当我点击集合视图单元格时,它会在true和false之间更新,但我的标签不会从added更新到not added。如果有任何帮助,我将不胜感激。我已经在这里工作了4天。我曾经阅读过苹果的文档,但没有给出如何使用布尔值实现这一点的示例。我希望在按下集合视图单元格时,集合视图单元格标签顶部的标签从“已添加”更改为“未添加”。我知道它在工作,因为当我用打印语句测试它时,当我按下集合视图单元格时,它会从true来回更改为false。我只是在标签从添加更改为不添加时遇到问题

我这里有我的集合视图控制器,当我点击集合视图单元格时,它会在true和false之间更新,但我的标签不会从added更新到not added。如果有任何帮助,我将不胜感激。我已经在这里工作了4天。我曾经阅读过苹果的文档,但没有给出如何使用布尔值实现这一点的示例。我希望在按下集合视图单元格时,集合视图单元格标签顶部的标签从“已添加”更改为“未添加”。我知道它在工作,因为当我用打印语句测试它时,当我按下集合视图单元格时,它会从true来回更改为false。我只是在标签从添加更改为不添加时遇到问题


import UIKit


class ShoppingListCollectionViewController: UICollectionViewController {

    var shoppingItemController = ShoppingItemController()

    var shoppingItemCollectionViewCell = ShoppingItemCollectionViewCell()
    override func viewDidLoad() {
        super.viewDidLoad()
        shoppingItemCollectionViewCell.updateViews()


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "ShoppingListDetailSegue" {

          guard let shoppingListDetailVC = segue.destination as? ShoppingListDetailViewController else {
                return 
            }

        }

        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }


    // MARK: UICollectionViewDataSource

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return shoppingItemController.shoppingItems.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ShoppingItemCell", for: indexPath) as? ShoppingItemCollectionViewCell else {
            fatalError("Collection view cell identifier is wrong or the cell is not a ShoppingItemCollectionViewCell")
        }

        // Configure the cell
        let shoppingListItem = shoppingItemController.shoppingItems[indexPath.item]
        cell.imageView.image = shoppingListItem.image
        cell.shoppingItemLabel.text = shoppingListItem.imageName
        if shoppingListItem.added == true {
            cell.hasBeenAddedLabel.text = "Added"
        } else {
            cell.hasBeenAddedLabel.text = "Not Added"
        }
        return cell
    }

    // MARK: UICollectionViewDelegate

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var chosenItem = shoppingItemController.shoppingItems[indexPath.item]
        chosenItem.added = !chosenItem.added
        shoppingItemController.shoppingItems[indexPath.item] = chosenItem
        if chosenItem.added == true {
            chosenItem.updateViews()
        }


        print(chosenItem.added)
    }






您只需从
didSelectItemAt
方法中的
cellForItem(at:)
方法获取单元格,并将标签文本更新为所需字符串:

override func collectionView(collectionView:UICollectionView,didSelectItemAt indepath:indepath){
将cell=collectionView.cellForItem(at:indexPath)设为!ShoppingItemCollectionViewCell
var chosenItem=shoppingItemController.shoppingItems[indexPath.item]
chosenItem.added.toggle()的
cell.label.text=chosenItem.added?“已添加”:“未添加”
}

Omg,真管用!非常感谢,那么我是否应该稍微扭曲一下逻辑,以便在再次选择时切换回来?