Reputation: 735
I am looking for a function or a loop prototype to add UILabels to a view on iPhone. The reason for this is I won't know in advance how many labels I need to add, so they will need to be added dynamically.
My pseudo code is as follows. The idea is that each label is given a string and then placed in the next screen, hence the +self.view.frame.size.width statement. Paging etc work perfectly, the problem is all the labels appear to be ending up on the second screen i.e. Label 3 appears on top of label 2. The issue would appear to be I am always referencing altLabel, and as such once I move to the second position, I am constantly referencing that position and never moving once there.
I can use the 'count' variable to multiple the screen width, but if I do that, every time I update the label text, it will overwrite the previous.
int count = 0;
int maxNumber = 10;
while(count < maxNumber) {
//Add a label
UILabel *altlabel; //Declare the label
if (count > 0) {
//Move the label
altlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(altlabel.frame)+self.view.frame.size.width,10,300,25)];
altlabel.text = [NSString stringWithFormat:@"%@ %@ (%d)", _name,_age, class+count];
}
else {
altlabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10,300,25)];
altlabel.text = [NSString stringWithFormat:@"%@ %@ (%d)", _name,_age, class];
}
altlabel.textColor = [UIColor greenColor];
[altlabel sizeToFit];
[_scrollView addSubview:altlabel];
count++;
}
Upvotes: 1
Views: 1469
Reputation: 1390
I think the problem will be that your label 'altLabel' is being reinitialised each time the loop runs through and therefore the frame of the label is also being reset.
This means that every time a new label is created, the frame will be in exactly the same place. A simple way to fix this would simply be to move the initial declaration of the label to outside the while loop.
A potentially better and more efficient fix would require a rework of the loop used. When I do things like this, I tend to use a for loop as you have use of an index to help position things:
for(int i = 0; i < 10; i++)
{
//Getting the x-position correct here may take a little trial and error
UILabel *altLabel = [[UILabel alloc] initWithFrame:CGRectMake(INSET + i*LABEL_WIDTH, 10, 300, 25)];
[altLabel setText:[NSString stringWithFormat:@"%@ %@ (%d)", _name,_age, class+count]];
[altLabel setTextColor:[UIColor greenColor]];
[altLabel sizeToFit];
[_scrollView addSubview:altLabel];
}
This should then mean you don't need a counter.
Hopefully this helps!
Matt
Upvotes: 0
Reputation: 6028
A much cleaner approach would be to predetermine the frame of your label before creation and then add it as a subview (or perhaps save each label to an array for reference later on).
NSInteger numberOfLabels = 10;
CGFloat labelHeight = 25.0;
CGFloat padding = 10.0;
for(NSInteger index = 0; index < numberOfLabels; ++index)
{
CGRect frame = CGRectMake(10.0, (index * (labelHeight + padding)), 300.0, labelHeight);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
[[self view] addSubview:label];
}
You may want to assign the label a tag with setTag:
to reference each label, or gather then from an array rather than looping through subviews to gather or append text after they have been created.
Upvotes: 0
Reputation: 3870
The problem is this line:
UILabel *altlabel; // Declare the label
if (count > 0) {
//Move the label
altlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(altlabel.frame)+self.view.frame.size.width,10,300,25)];
altlabel.text = [NSString stringWithFormat:@"%@ %@ (%d)", _name,_age, class+count];
}
You are setting up the frame using altlabel.frame, but there altlabel is not setted up: you redeclared it on the first line with UILabel *altlabel.
With this code every label but the first will have the same frame. Try with this:
int count = 0;
int maxNumber = 10;
CGRect rect;
while(count < maxNumber) {
// Add a label
UILabel *altlabel; // Declare the label
if (count > 0) {
//Move the label
altlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(rect.frame)+self.view.frame.size.width*count, 10, 300, 25)];
altlabel.text = [NSString stringWithFormat:@"%@ %@ (%d)", _name,_age, class+count];
} else {
altlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 25)];
altlabel.text = [NSString stringWithFormat:@"%@ %@ (%d)", _name,_age, class];
}
rect = altlabel.frame;
altlabel.textColor = [UIColor greenColor];
[altlabel sizeToFit];
[_scrollView addSubview:altlabel];
count++;
}
Now the frame of new label is saved in a temporary var (CGRect frame) and you can use it.
Upvotes: 1
Reputation: 735
This code should work:
int count = 0; int maxNumber = 10;
while(count < maxNumber) {
//Add a label
UILabel *altlabel; //Declare the label
if (count > 0) {
//Move the label
altlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(altlabel.frame)+(self.view.frame.size.width*count),10,300,25)];
altlabel.text = [NSString stringWithFormat:@"%@ %@ (%d)", _name,_age, class+count];
}
else {
altlabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10,300,25)];
altlabel.text = [NSString stringWithFormat:@"%@ %@ (%d)", _name,_age, class];
}
altlabel.textColor = [UIColor greenColor];
[altlabel sizeToFit];
[_scrollView addSubview:altlabel];
count++;
}
Upvotes: 0