
[英]How to display image of selected UICollectioNViewCell in UIView? (Swift 3)
[英]drawRect in UIView class not called when custom UICollectionViewCell is selected in swift
我在自定义collectionView单元上有一个子类的UIView。 它显示正常,但在ViewController的“ didSelect”委托方法中,我为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中设置了checked属性。 属性中的setNeedsDisplay不会失败,但是drawRect将不再被调用。
**更新**不需要初始化函数,应将其删除。
解决了!! 我知道这与单元格引用有关。 在研究问题时,我意识到在collectionView didSelectItemAtIndexPath和didDeselectItemAtIndexPath中,我正在引用可重复使用的单元格,该单元格仅对视图中的单元格有效,并且仅在cellForItemAtIndexPath中进行更新:
let selectedCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! ImageDataCell
这将选择适当的单元格,但不执行任何操作。 相反,我需要引用实际的单元格:
let selectedCell = collectionView.cellForItemAtIndexPath(indexPath) as! ImageDataCell
现在,执行此方法时,视图在显示时将刷新。 您仍然需要在cellForItemAtIndexPath中设置单元格,因为这会刷新移回到框架中的单元格,但不会刷新正在选择或取消选择的单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.