1
votes

I have a UIView subclassed on a custom collectionView cell. It displays fine but in the "didSelect" delegate method in my ViewController I set a property for the UIView which refreshes with a setNeedsDisplay and drawRect isn't being fired. Does it have anything to do with the dequeueReusableCell? How would that be coded? This is in swift 1.2 not 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)
        }
    }

This is the UIView class. the checked property is being set in the collectionView didSelectCell. The setNeedsDisplay in the property doesn't fail but drawRect will never be called again.

** UPDATE ** The init functions are not required and should be removed.

1
In the viewController I set a property in the UIView class which executes the setNeedsDisplay. The drawRect isn't executed.cheborneck
The setNeedsDisplay is executed but the drawRect is only triggered when the class is initializedcheborneck
I tried to post a code snippet but couldn't get it formatted.cheborneck
Yeah it says put four spaces in front of a line. Code and pre didn't work.cheborneck

1 Answers

0
votes

SOLVED!! I knew it had something to do with cell reference. In researching the problem i realized that in collectionView didSelectItemAtIndexPath and didDeselectItemAtIndexPath I was referencing the reusable cell which is only valid for cells in view and are only updated in cellForItemAtIndexPath:

let selectedCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! ImageDataCell

which would select the appropriate cell but do nothing with it. Instead I needed to reference the actual cell:

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

now the view is refreshed while displayed when this method is executed. You still need to set the cell in cellForItemAtIndexPath because that refreshes cells that move back into frame but doesn't refresh a cell being selected or deselected.