试图覆盖“已选择”在UICollectionViewCell Swift中进行自定义选择状态 [英] Trying to override "selected" in UICollectionViewCell Swift for custom selection state

查看:1098
本文介绍了试图覆盖“已选择”在UICollectionViewCell Swift中进行自定义选择状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在UICollectionView中为我的单元格实现自定义选择样式。尽管很容易在didSelect和didDeSelect方法中手动执行此操作,但我希望通过操作UICollectionViewCell中的selected变量来实现此目的。

I am trying to implement a custom selection style for my cells in a UICollectionView. Even though it is easily possible to do this manually in the didSelect and didDeSelect methods I would like to achieve this by manipulating the "selected" variable in UICollectionViewCell.

我有这个代码:

    override var selected: Bool {
    get {
        return super.selected
    }
    set {
        if newValue {
            self.imageView.alpha = 0.5
            println("selected")
        } else if newValue == false {
            self.imageView.alpha = 1.0
            println("deselected")
        }
    }
}

现在,当我选择一个单元格时,单元格会突出显示,但已选中会被打印两次,取消选择不起作用(即使实现了两个UICollectionView方法)。

Now, when I select a cell, the cell gets highlighted but "selected" gets printed twice and the deselection does not work (even though both UICollectionView methods are implemented).

我该如何解决这个问题?谢谢!

How would I go about this? Thanks!

推荐答案

对于Swift 3.0:

And for Swift 3.0:

override var isSelected: Bool {
    didSet {
        alpha = isSelected ? 0.5 : 1.0
    }
}

这篇关于试图覆盖“已选择”在UICollectionViewCell Swift中进行自定义选择状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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