Can't stack views correctly in a NSClipView

79 views Asked by At

I'm getting mad at this code because it just shows the last view (in it's correct position) and it forgets to paint the others...

// Gets a stack of devices
let volumes = getDevices()
var index = 0
for device in volumes {
    let volume = devicePrototype
    // devicePrototype it's a custom view
    volume?.setName(name: device.name) // setting the label for the name
    volume?.setIcon(icon: device.icon) // setting the image for the icon
    // Setting the position of the view (width = 600, height = 105)
    // and all the views have the same size.
    volume?.layer?.frame = CGRect(x: 0, y: index * 105, width: 600, height: 105)
    // Adding the view to the NSClipView (Here's the problem :P)
    devicesStack.addSubview(volume!)
    index += 1
}

Can you help me find the problem please?

1

There are 1 answers

2
Anusha Kottiyal On

I think you are creating only one view (device) and modifying the same through the loop. You are referencing same devicePrototype object repeatedly and changing the properties only. Please check that.

Try to initialize a new object in each iteration,

let volume = DevicePrototype()