Newbee
Newbee

Reputation: 3301

how to add multiple labels to a view programmatically without worrying about its Y position?

I have a situation here, I want to add user name with a check button in a view, looks simple :)...

Question is I may have single or multiple user, so I need to add it through code only, I want to add all names like a ladder with its corresponding check button.

What is the best way to do that?

I have tried by adding labels for each user.. While adding I wanted to set its top position one by one to make it aligned properly.. Do I have to calculate top like this? Any easiest way is there.. Like how they are doing in Android (relative Layout).

thanks

Upvotes: 1

Views: 1768

Answers (3)

DJB
DJB

Reputation: 867

You can create array of usernames and use following code. This is test code for your reference.

NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:@"1"];
[arr addObject:@"2"];
[arr addObject:@"3"];
[arr addObject:@"4"];
[arr addObject:@"15"];

Now looping:

for (int i = 0; i<[arr count]; i++) {

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10*10*i, 100, 40)];
  label.text = [arr objectAtIndex:i]; //usernames from array
  [self.view addSubview:label];
}

Upvotes: 2

Bergasms
Bergasms

Reputation: 2203

I would use a UItableView with a custom cell. The cell contains your input fields. This way you can have as many inputs as you like and they will always be layed out in order in the same way, and you don't need to write any funky code. You also get scrolling and memory management for free.

Upvotes: 1

foundry
foundry

Reputation: 31745

The Objective-C equivalent of relativelayout is Cocoa Auto Layout

Upvotes: 1

Related Questions