Iphone 在循环中动态添加UILabel
我正在寻找一个函数或循环原型,将UILabel添加到iPhone上的视图中。原因是我不知道我需要添加多少标签,所以需要动态添加标签 我的伪代码如下。其思想是给每个标签一个字符串,然后将其放置在下一个屏幕中,从而得到+self.view.frame.size.width语句。分页等工作正常,问题是所有标签似乎都出现在第二个屏幕上,即标签3出现在标签2的顶部。问题似乎是我总是在引用altLabel,因此,一旦我移动到第二个位置,我就会不断地引用该位置,并且从未移动过一次 我可以使用“count”变量乘以屏幕宽度,但如果这样做,每次更新标签文本时,它都会覆盖上一个Iphone 在循环中动态添加UILabel,iphone,ios,objective-c,Iphone,Ios,Objective C,我正在寻找一个函数或循环原型,将UILabel添加到iPhone上的视图中。原因是我不知道我需要添加多少标签,所以需要动态添加标签 我的伪代码如下。其思想是给每个标签一个字符串,然后将其放置在下一个屏幕中,从而得到+self.view.frame.size.width语句。分页等工作正常,问题是所有标签似乎都出现在第二个屏幕上,即标签3出现在标签2的顶部。问题似乎是我总是在引用altLabel,因此,一旦我移动到第二个位置,我就会不断地引用该位置,并且从未移动过一次 我可以使用“count”变量
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++;
}
int count=0;
int maxNumber=10;
while(计数<最大数){
//添加标签
UILabel*altlabel;//声明标签
如果(计数>0){
//移动标签
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(altlabel.frame)+self.view.frame.size.width,10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,_name,_age,class+count];
}
否则{
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(10,10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,名称、年龄、类别];
}
altlabel.textColor=[UIColor greenColor];
[altlabel sizeToFit];
[_scrollviewaddsubview:altlabel];
计数++;
}
此代码应适用于:
整数计数=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++;
}
while(计数0){
//移动标签
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(altlabel.frame)+(self.view.frame.size.width*count),10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,_name,_age,class+count];
}
否则{
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(10,10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,名称、年龄、类别];
}
altlabel.textColor=[UIColor greenColor];
[altlabel sizeToFit];
[_scrollviewaddsubview:altlabel];
计数++;
}
此代码应适用于:
整数计数=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++;
}
while(计数0){
//移动标签
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(altlabel.frame)+(self.view.frame.size.width*count),10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,_name,_age,class+count];
}
否则{
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(10,10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,名称、年龄、类别];
}
altlabel.textColor=[UIColor greenColor];
[altlabel sizeToFit];
[_scrollviewaddsubview:altlabel];
计数++;
}
问题在于这一行:
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];
}
您正在使用altlabel设置框架。框架,但未设置altlabel:您使用UILabel*altlabel在第一行重新声明了框架
使用此代码,除第一个标签外,每个标签都将具有相同的框架。试试这个:
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++;
}
int count=0;
int maxNumber=10;
CGRect rect;
while(计数<最大数){
//添加标签
UILabel*altlabel;//声明标签
如果(计数>0){
//移动标签
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(rect.frame)+self.view.frame.size.width*count,10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,_name,_age,class+count];
}否则{
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(10,10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,名称、年龄、类别];
}
rect=altlabel.frame;
altlabel.textColor=[UIColor greenColor];
[altlabel sizeToFit];
[_scrollviewaddsubview:altlabel];
计数++;
}
现在,新标签的框架保存在一个临时变量(CGRect框架)中,您可以使用它。问题是这一行:
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];
}
您正在使用altlabel设置框架。框架,但未设置altlabel:您使用UILabel*altlabel在第一行重新声明了框架
使用此代码,除第一个标签外,每个标签都将具有相同的框架。试试这个:
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++;
}
int count=0;
int maxNumber=10;
CGRect rect;
while(计数<最大数){
//添加标签
UILabel*altlabel;//声明标签
如果(计数>0){
//移动标签
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(rect.frame)+self.view.frame.size.width*count,10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,_name,_age,class+count];
}否则{
altlabel=[[UILabel alloc]initWithFrame:CGRectMake(10,10300,25)];
altlabel.text=[NSString stringWithFormat:@“%@%@(%d)”,名称、年龄、类别];
}
rect=altlabel.frame;
altlabel.textColor=[UIColor greenColor];
[altlabel sizeToFit];
[_scrollviewaddsubview:altlabel];
计数++;
}
现在,新标签的框架保存在一个临时变量(CGRect框架)中,您可以使用它。一个更干净的方法是在创建之前预先确定标签的框架,然后将其添加为子视图(或者可能将每个标签保存到一个数组中以供以后参考)
NSInteger numberOfLabels=10;
CGFloat labelHeight=25.0;
CGFloat padding=10.0;
对于(NSInteger index=0;index