gpt4 book ai didi

swift - 在 swift 中选择自定义 UICollectionViewCell 时,不会调用 UIView 类中的 drawRect

转载 作者:行者123 更新时间:2023-11-28 08:59:29 25 4
gpt4 key购买 nike

我有一个在自定义 collectionView 单元格上子类化的 UIView。它显示正常,但在我的 ViewController 的“didSelect”委托(delegate)方法中,我为 UIView 设置了一个属性,该属性使用 setNeedsDisplay 刷新并且没有触发 drawRect。它与dequeueReusableCell有什么关系吗?那将如何编码?这是 swift 1.2 而不是 obj-c。

class setSelection: UIView {

private var _checked: Bool = false

override init(frame: CGRect) {
super.init(frame: frame)
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setNeedsDisplay()
}

override func drawRect(rect: CGRect) {
super.drawRect(rect)
. . . code . . .
}

var checked: Bool {
get {
return _checked
}
set {
_checked = newValue
self.setNeedsDisplay()
println(_checked)
}
}

这是 UIView 类。选中的属性正在 collectionView didSelectCell 中设置。属性中的 setNeedsDisplay 不会失败,但 drawRect 将永远不会被再次调用。

** 更新 ** init 函数不是必需的,应将其删除。

最佳答案

解决了!!我知道它与细胞引用有关。在研究这个问题时,我意识到在 collectionView didSelectItemAtIndexPath 和 didDeselectItemAtIndexPath 中我引用了可重用单元格,它仅对 View 中的单元格有效并且仅在 cellForItemAtIndexPath 中更新:

让 selectedCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) 作为!图片数据单元格

它会选择适当的单元格但不对其进行任何操作。相反,我需要引用实际的单元格:

let selectedCell = collectionView.cellForItemAtIndexPath(indexPath) as! ImageDataCell

现在执行此方法时, View 会在显示时刷新。您仍然需要在 cellForItemAtIndexPath 中设置单元格,因为这会刷新移回到框架中的单元格,但不会刷新被选中或取消选中的单元格。

关于swift - 在 swift 中选择自定义 UICollectionViewCell 时,不会调用 UIView 类中的 drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32281468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com