Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Automatically creating a UILabel

I want to create UILabels in swift automatically based on need. I.e If my data base request returns 4 values, I want 4 labels, if it returns 1 value, i want 1 label..ect.

So far i have the code below, but it displays my UILabels on-top of each other. I use a 'var spacer' to move the new Labels down by 50 but it doesn't seem to work.

 for comments in object["Comments"] as NSArray {
         var counter = ""   
         println(comments)
         counter = comments as String

           var spacer: CGFloat = 50
            var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
            label.center = CGPointMake(160, 300 + spacer )
            label.textAlignment = NSTextAlignment.Center
            label.text = counter
            self.view.addSubview(label)
            spacer = spacer + 50


       }

How can i do this?

Thanks Youseef