lundi 1 juin 2015

custom checkbox in uicollectionviewcell swift ios

hi i working on food app, the app has foods in collectionviewcell and i added checkbox in cell so how i can select some foods from cell by checkbox , i add the checkbox in collection view cell and add class for button but when i click on checkbox in first cell the all other cells is selected,

the checkbox code

    var isCheckedGlobal = Bool() // !! Global Variable // You might need to change '= Bool()' to '= false' or '= true'

class CheckBox: UIButton {

    //images

    let checkedImage = UIImage(named: "checked") as UIImage?
    let unCheckedImage = UIImage(named: "unchecked")as UIImage?


    //bool propety
    var isChecked:Bool = false{
        didSet{
            if isChecked == true{
                self.setImage(checkedImage, forState: .Normal)
            }else{
                self.setImage(unCheckedImage, forState: .Normal)
            }
        }
    }

    override func awakeFromNib() {
        self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        self.isChecked = false
    }

    func buttonClicked(sender:UIButton) {
        if(sender == self){
            if isChecked == true{
                isChecked = false
                isCheckedGlobal = false // !! Set variable's value
            }else{
                isChecked = true
                isCheckedGlobal = true // !! Set variable's value
            }
        }
    }

}

and in collectionview this code

   if let foodcodes = self.menu![indexPath.row]["code"] as? NSString {



        if isCheckedGlobal == false {

            cell.foodNumber.enabled = false
            cell.foodNumber.text = ""

        } else {

            if cell.foodNumber.tag == cell.checkboxx.tag {
            cell.foodNumber.enabled = true
            cell.foodNumber.text = "1"


            }

        }


    }




Aucun commentaire:

Enregistrer un commentaire