我有一个简单的设置。有一个按钮作为拨动开关。如果已启用,则用户应该只需点击它们即可删除照片。如果未启用,则点击时不会发生任何事情。
我最初将切换变量remove
设置为false。点击时,按钮会更改图像并将remove
更改为true。
var remove = false
@IBAction func removeButton(sender: AnyObject) {
if remove == false {
let image1 = UIImage(named: "Minus_sign_of_a_line_in_horizontal_position_64 (2)") as UIImage!
removeButtonAppearance.setImage(image1, forState: .Normal)
remove = true
} else if remove == true {
let image2 = UIImage(named: "Minus_sign_of_a_line_in_horizontal_position_64") as UIImage!
removeButtonAppearance.setImage(image2, forState: .Normal)
remove = false
}
}
快进到我的UICollectionView
代码。
使用didSelectItemAtIndexPath()
函数,检查remove
是否为真,如果是,我尝试清空单元格并重新加载UICollectionView
的数据
这是我遇到问题的地方。简单地说,点击时没有任何反应。但是,如果我删除remove
的检查,则可以正常使用。我做错了什么?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if collectionView == collectionView2 {
let cell: ConfirmRequestsCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ConfirmRequestsCollectionViewCell
if remove == true {
requestsArray.removeAtIndex(indexPath.row)
images2.removeAtIndex(indexPath.row)
self.collectionView2.reloadData()
}
} else {
let cell: ConfirmOffersCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! ConfirmOffersCollectionViewCell
if remove == true {
offersArray.removeAtIndex(indexPath.row)
images1.removeAtIndex(indexPath.row)
self.collectionView1.reloadData()
}
}
}
答案 0 :(得分:0)
我的remove
变量是本地的。我将它切换到全局,现在一切正常!