2015-06-19 54 views
-1

你好這個問題已經提出不止一次在這裏更多,但我無法找到幫助 我的問題在UICollectionViewCell 孔加入複選框時,我已經嘗試了很多辦法,但無濟於事 此圖片顯示了位於UICollectionViewCell複選框在UICollectionViewCell

enter image description here

好吧,當我點擊複選框內,這是他的選擇,但食物的其餘部分也被選中,這是我想做的事情,點擊複選框裏面的時候它在單元格中選擇框並更新UICollectionView

自定義複選框代碼

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

} 

cellForItemAtIndexPath代碼

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ordercell", forIndexPath: indexPath) as! SuperStarOrdersCollectionViewCell 

     if isCheckedGlobal == true{ 
      println("Checked Cell \(indexPath.row)") 
     } 
     else{  
      println("unChecked Cell \(indexPath.row)") 
     } 
     return cell 
    } 

回答

2

你的,你想要做什麼的描述是混亂的。

我猜你只想要複選框來改變當前項目。因此,如果用戶點擊列表中的第二個項目,該項目將被檢查,但沒有其他項目。正確?

在這種情況下,請勿使用全局。

您需要維護有關集合視圖中每個單元格的信息數組。這可以是一系列結構。說它是一個結構數組,結構的一個屬性是一個布爾isChecked。

當用戶點擊複選框時,使用當前單元格的indexPath索引到您的結構數組中並切換isChecked布爾值。然後在你的cellForItemAtIndexPath中使用該結構來配置你的單元格,包括使用isChecked布爾值來檢查/取消選中複選標記。

+0

是的,這就是我的意思,你可以解釋更多或爲我輸入代碼。 –

+0

輸入你的代碼?不,我不打算爲你做你的工作,除非你想聘請我這樣做。如果您嘗試實施我所描述的內容並在您遇到困難時報告回來,那麼該如何? –