2015-06-01 94 views
1

嗨我在食物應用程序,該應用程序有collectviewcell中的食物,我在單元格中添加了複選框,所以我如何從複選框中選擇一些食物從單元格, 我添加複選框集合視圖單元格和添加類按鈕,但是當我在第一小區複選框點擊所有其他小區被選擇時,自定義複選框在uicollectionviewcell swift ios

複選框代碼

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

} 

和在此的CollectionView代碼

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" 


      } 

     } 


    } 

回答

0

1.首先建立一個全球性的可變數組一樣arrSelectedCells

2.Now在UICollectionView cellForRow設置indexpath.row值作爲標籤複選框按鈕。

3.In buttonClicked行動,擺脫按鍵的標記值和標籤值存儲在arrSelectedCells並檢查標籤存在於arrSelectedCells如果它存在,則刪除it.So多個相同的標記值中不存在的arrSelectedCells

4.現在,UICollectionView cellForRow,檢查indexpath.row值是否存在於arrSelectedCells如果存在,則啓用複選框,否則禁用複選框。