簡體   English   中英

uicollectionviewcell swift ios中的自定義復選框

[英]custom checkbox in uicollectionviewcell swift ios

嗨,我在食品應用程序上工作,該應用程序在collectionviewcell中有食品,並且在單元格中添加了復選框,因此我如何通過復選框從單元格中選擇一些食品,我在集合視圖單元格中添加了該復選框,並為按鈕添加了類,但是當我單擊該復選框時在第一個單元格中,選擇所有其他單元格,

復選框代碼

    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
            }
        }
    }

}

並在集合中查看此代碼

   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"


            }

        }


    }

1,首先創建一個全局可變數組,如arrSelectedCells

2.現在在UICollectionView cellForRow中將indexpath.row值設置為復選框按鈕的標記。

3,在buttonClicked動作中,從按鈕獲取標簽值,並將標簽值存儲在arrSelectedCells然后檢查標簽是否存在於arrSelectedCells ,然后將其刪除,以便在arrSelectedCells中不存在多個相同值的標簽。

4.Now, UICollectionView cellForRow ,檢查是否indexpath.row值存在於arrSelectedCells如果存在,則使復選框否則禁用的復選框。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM