2015-06-19 58 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

输入你的代码?不,我不打算为你做你的工作,除非你想聘请我这样做。如果您尝试实施我所描述的内容并在您遇到困难时报告回来,那么该如何? –