Deactivate items in UICollection View

0

I have a UICollectionView where buttons are created

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (self.myGrid!.HEIGHT * self.myGrid!.WIDTH)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let identifier = "item"

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! CollectionViewCell
        //print("Posicion" , indexPath)
        cell.btnCollection.setTitle(String(describing: indexPath), for: UIControlState.normal)
        cell.btnCollection.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        return cell
    }

    @objc func buttonAction(sender: UIButton!) {
        var firePositionX,firePositionY:Int
        var position = String(describing: sender.currentTitle).components(separatedBy: ", ")
        var x = (String(position[1]).dropLast() as NSString).integerValue
        //init firePosition
        firePositionX=x%5
        firePositionY=x/5
        //We are fireing
        var fireResult:Bool = (myGrid?.fire(x: firePositionX, y: firePositionY))!
        if(fireResult){
            sender.backgroundColor = UIColor.red
            sender.setTitleColor(UIColor.red, for: .normal)
            coupFeuReussi = coupFeuReussi + 1
            if coupFeuReussi == (myGrid?.numCase)! {
                print("Ganaste")
            }
        }else{
            sender.backgroundColor = UIColor.blue
            sender.setTitleColor(UIColor.blue, for: .normal)
            coup = coup - 1
            tirs.text = "tirs : \(coup)"
            if coup == 0 && coupFeuReussi < (myGrid?.numCase)! {

                let alert = UIAlertController(title: "Tu veux recommencer", message: "Tu n'as pas tiré tous les bateux.", preferredStyle: .alert)
                //let recommencer = UIAlertAction(title: "Yes", style: .default, handler: nil)
                //alert.addAction(recommencer)
                //alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
                self.present(alert, animated: true)
                collectionView.visibleCells = false
            }
        }
    }

I would like to know if there is any function to block the buttons in the final part when the game is over

    
asked by Ernesto Emmanuel Yah Lopez 06.02.2018 в 19:01
source

1 answer

0

To be able to access the objects of a certain cell you have to do something like this:

let index_final = IndexPath(row: row_final, section: section_final)
if let celda = self.collection_view.cellForItem(at: index_final)
{
    let cell = celda as! CollectionViewCell
    cell.btnCollection.isEnabled = false
}

where "index_final" is the index of the cell in which the buttons you want to disable are located.

    
answered by 06.02.2018 в 21:10