Ios UICollectionViewCell中的复选框
你好,这个问题在这里已经提过不止一次了,但是我找不到帮助 我的问题是在UICollectionViewCell中添加复选框时 嗯,我试过很多方法,但都没有用 此图显示UICollectionViewCell中的内容 嗯,当我在复选框内单击时,这是他的选择,但其余的食物也被选中,我想这样做,当它在单元格中选择box并更新UICollectionView时,在复选框内单击 自定义复选框代码Ios UICollectionViewCell中的复选框,ios,swift,checkbox,uicollectionview,Ios,Swift,Checkbox,Uicollectionview,你好,这个问题在这里已经提过不止一次了,但是我找不到帮助 我的问题是在UICollectionViewCell中添加复选框时 嗯,我试过很多方法,但都没有用 此图显示UICollectionViewCell中的内容 嗯,当我在复选框内单击时,这是他的选择,但其余的食物也被选中,我想这样做,当它在单元格中选择box并更新UICollectionView时,在复选框内单击 自定义复选框代码 var isCheckedGlobal = Bool() // !! Global Variable //
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
}
你对你想做什么的描述令人困惑 我猜您只希望复选框更改当前项。因此,如果用户点击列表中的第二项,则会选中该项,但不会选中其他项。对吗 在这种情况下,不要使用全局变量 您需要维护集合视图中每个单元格的信息数组。可以是一个结构数组。假设它是一个结构数组,并且该结构的一个属性是布尔值
当用户点击复选框时,使用当前单元格的indexPath索引到结构数组中,并切换isChecked布尔值。然后在cellForItemAtIndexPath中使用结构来配置单元格,包括使用isChecked布尔值来选中/取消选中复选标记。为您键入代码?不,我不会为你做你的工作,除非你想雇我来做。如果你陷入困境,试着实现我所描述的,并向我汇报,怎么样?