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

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

问题描述

我正在尝试为 UICollectionView 中的单元格实现自定义选择样式.尽管可以很容易地在 didSelect 和 didDeSelect 方法中手动执行此操作,但我想通过操作 UICollectionViewCell 中的选定"变量来实现这一点.

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).

我该怎么做呢?谢谢!

推荐答案

对于 Swift 3.0:

And for Swift 3.0:

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

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

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