如果isSelected从未为true,则执行UICollectionViewCell的语句

时间:2018-11-21 22:24:02

标签: ios swift uicollectionview uicollectionviewcell

我有一家商店作为UICollectionView。我有一个突出显示用户将使用的当前球的系统。那里的单元格可以拥有或不拥有。当我单击不归所有时,如果用户没有足够的钱来购买它,则什么也不会发生,并且如果这样做,isSelected应该设置为true。问题是,当我单击锁定的项目时,它仍会突出显示,但是没有断点会在if语句中触发以测试是否已购买。我在isSelected上有一个didSet以突出显示该单元格。

这是我的代表班

这是didSelectItemAt函数的简化版本:

//I have an array of structs, called balls, to hold values of each item.


    indx = indexPath.item//Universal variable that can be accessed in both this class and the cell class

    let cell = collectionView.cellForItem(at: indexPath) as! Shop1CollectionViewCell

    balls[indexPath.item].owned =  cell.clickedOn(price: balls[indexPath.item].price, index: indexPath.item)
//saves the value to know if you own that item and can select it whenever if you buy it it returns true if not, false

    if balls[indexPath.item].owned == true && cell.owned == true{
        //This will only pass if the item has been bought, so you can not select items that you don't own
        if ogCell.isSelected{
            ogCell.isSelected = false
         //deselects the saved cell, last selected from last time, that is saved from userdefaults

        }
    currentItem = indexPath.item//reference for the current item that is selected

       cell.isSelected = true
//this will trigger the highlight() function in didSet

        lastCell = cell//reference to last cell selected
        lastIndex = indexPath.item//index of that cell

    }

这是我的一些细胞班:

//This is what is executed when the isSelected is changed.

override var isSelected: Bool{
    didSet{
        if self.isSelected{

        highlight()

        }else{
            unHighlight()
        }

}
}


//This is what is executed when the cell is clicked on:


if money >= price && !owned{
        money -= price
        owned = true
        isSelected = true
    }else if owned == false{
        owned = false
        isSelected = false
        currentItem = lastIndex

        lastCell.highlight()
    }

当我单击该单元格时,我创建了一种方法来取消选择所选单元格(如果它不属于该单元格),但是此过程取消了先前的选择,因此我一无所获。我想知道为什么突出显示未拥有的单元格,即使调试说它从未经过isSelected设置为true的任何时候。

如果需要,我可以添加更多代码,但其他所有代码似乎都是不必要的。

0 个答案:

没有答案