Kieran Crown
Kieran Crown

Reputation: 317

Variables being reset in NSCollectionViewItem

I'm trying to update a NSTextField title inside of a NSCollectionViewItem from a parent NSCollectionViewDataSource. Here is my code.

NSCollectionViewDataSource

    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
        let cell = collectionView.makeItem(
          withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "MacDeviceItem"),
          for: indexPath
        ) as! MacDeviceItem

        let device = devicesList[indexPath.item]
        cell.deviceName = "Hello World"
        return cell
    }

NSCollectionViewItem

class MacDeviceItem: NSCollectionViewItem {
    dynamic public var deviceName:String = "Unknown Device Name"

    @IBOutlet weak var deviceImage: NSImageView!
    @IBOutlet weak var deviceNameLabel: NSTextField!
    @IBOutlet weak var deviceStatusLabel: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        view.wantsLayer = true
        updateSelection()
    }

    override func viewDidLayout() {
        super.viewDidLayout()
        view.layer?.cornerRadius = 7
        print(deviceName)
    }

    override func viewDidAppear() {
        self.deviceNameLabel?.stringValue = deviceName
    }

    private var selectionColor : CGColor {
        let selectionColor : NSColor = (isSelected ? .controlAccentColor : .clear)
        return selectionColor.cgColor
    }

    private var selectionTextColor : NSColor {
        let selectionTextColor : NSColor = (isSelected ? .selectedMenuItemTextColor : .controlTextColor)
        return selectionTextColor
    }

    override var isSelected: Bool {
        didSet {
            super.isSelected = isSelected
            updateSelection()
            // Do other stuff if needed
        }
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        updateSelection()
    }

    private func updateSelection() {
        view.layer?.backgroundColor = self.selectionColor
        deviceNameLabel?.textColor = self.selectionTextColor
        deviceStatusLabel?.textColor = self.selectionTextColor
    }

}

If I print the value of deviceName on viewDidLoad the value is there. However when trying to set the label in ViewDidAppear or nothing happens and the variable has been reset back to the default value. I'm pretty new to Swift but have had some previous experience in Objective-C but don't remember having this issue.

Upvotes: 0

Views: 150

Answers (1)

Frankenstein
Frankenstein

Reputation: 16361

Here you don't actually need the variable deviceName: String. You could directly set the value to the deviceNameLabel: NSTextField, like this:

cell.deviceNameLabel.stringValue = "Hello World"

If you actually need the variable you could try the didSet approach, like this:

public var deviceName:String = "Unknown Device Name" {
    didSet {
        cell.deviceNameLabel.stringValue = deviceName
    }
}

Upvotes: 0

Related Questions