在点击时切换 UICollectionView 单元格的选择/取消选择状态 - Swift [英] Toggle select / deselect state of a UICollectionView Cell on tap - Swift

查看:85
本文介绍了在点击时切换 UICollectionView 单元格的选择/取消选择状态 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以首先,我已经坚持了几天,花了一整天的时间阅读并尝试了 Stack Overflow 上的许多选项,但没有成功

So first of all i've been stuck on this for a few days and spent a full day reading and trying many options on Stack Overflow already but non to my success

我正在尝试完成的工作听起来很简单,并且查看 Apple 文档在我看来它应该可以工作https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegate_protocol/#//apple_ref/occ/intfm/UICollectionViewDelegate/collectionView:shouldHighlightItemAtIndexPath:

What i'm trying to accomplish sounds simple and going over the Apple documentation it seems to me it should work https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegate_protocol/#//apple_ref/occ/intfm/UICollectionViewDelegate/collectionView:shouldHighlightItemAtIndexPath:

基本上我想要实现的是在点击时切换 UICollectionView 单元格的选定状态.

Basically what i'm trying to achieve is to toggle the selected state of a UICollectionView Cell on tap.

第一次点击 - 将单元格置于选中状态并将背景颜色更改为白色.

First tap - Send the cell into a selected state and change background colour to white.

第二次点击 - 使单元格进入取消选择状态并将背景颜色更改为清除

Second tap - Send the cell into a deselected state and change background colour to clear

视图控制器 -

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as? CollectionViewCell {
        cell.cellImage.image = UIImage(named: images[indexPath.row])
        return cell
    } else {
        return CollectionViewCell()
    }
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell {
        cell.toggleSelectedState()
    }
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? CollectionViewCell {
        cell.toggleSelectedState()
    }
}

单元格 -

    func toggleSelectedState() {
    if selected {
        print("Selected")
        backgroundColor = UIColor.whiteColor()
    } else {
        backgroundColor = UIColor.clearColor()
        print("Deselected")
    }
}

我遇到的问题是在点击已选择的单元格时没有调用 didDeselectItemAtIndexPath,但如果我点击另一个单元格,它将被调用并选择新单元格...

The problem i'm having is the didDeselectItemAtIndexPath is not being called when tapping on a cell thats already selected, Though if i tap another cell it will get called and selects the new cell...

我已尝试在 shouldSelectItemAtIndexPath 中检查所选状态 &shouldDeselectItemAtIndexPath,我什至尝试编写一个 tapGesture 来解决这个问题,但仍然没有运气...

I have tried checking for selected states in shouldSelectItemAtIndexPath & shouldDeselectItemAtIndexPath, i even tried writing a tapGesture to get around this and still no luck...

有什么我遗漏的吗?或者是否有任何已知的解决方法?任何帮助将不胜感激!

Is there something i'm missing? Or is there any known work arounds to this? Any help would be greatly appreciated!

推荐答案

也许你可以创建一个与单元格边界相同的 UIButton(),并标识按钮中的选择.然后在按钮的点击动作中,您可以执行一些操作来取消选择"已选择"单元格.

Maybe you can create a UIButton() with the same bounds as the cell, and identify the select in the button. Then in the tap action of the button, you can do something to 'disselect' the cell 'selected'.

这篇关于在点击时切换 UICollectionView 单元格的选择/取消选择状态 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆