0

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?

Cristian
  • 654
  • 1
  • 13
  • 30

1 Answers1

0

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()
Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
  • It doesn't work, because I created the interface using a custom view in the interface builder and I connected its labels and image views to its own class using an outlet. So the point is: if I initialize just the class, I won't be able to use the Interface : ( and my solution was to crate an outlet from the custom view in the app delegate (named devicePrototype) so I could create instances like this one... How do I create multiple instances of something build in the interface builder? – Cristian Jan 07 '17 at 12:50
  • Create custom view classes and use its instances. You can connect the classes to your interface builder views. – Anusha Kottiyal Jan 07 '17 at 15:52