从 NSMutableArray 动态显示 UILabel
Displaying UILabel dynamcially from the NSMutableArray
我有一个可变数组如下
labelArrays = [NSMutableArray arrayWithObjects:@"One”, @“Two”, @“Three", nil];
我想将所有列表数组值显示为单独的标签。这就像使用数组值动态显示 UILabel
一样。我所做的如下,
int count=20;
for(int i = 0; i < [labelArrays count]; i++){
label1 = [[UILabel alloc]init];
label1.text = [labelArrays componentsJoinedByString:@" "];
label1.textColor = [UIColor colorWithRed:0.643 green:0.643 blue:0.639 alpha:1];
label1.layer.borderWidth = 1.0;
label1.layer.cornerRadius = 8;
count+=20;
[self addSubview:label1];
}
这里我只能显示单个标签内的所有数组值。如何动态显示多个标签中的数组值。
//viewcontroller
myView.label1.frame =CGRectMake(588,200,200,28);
只是一个想法 - 使用 tableview 来显示数组不是更好吗?
看起来您可能只需要处理它们在视图中的位置,因为代码看起来是在添加标签,但实际上是将它们放在彼此之上。
你的编码没问题,但你没有添加框架,试试这个
label1 = [[UILabel alloc] initWithFrame: CGRectMake(0,count,aslikeyourwidth,aslikeyourheight)];
为 view
定义一个 public 方法,您愿意在其中添加 UILabel
,将 CGRect
传递给该方法,然后根据您的设计,每次迭代更新 x 坐标或 y 坐标。按照以下步骤操作:
// code in your viewcontroller
[myView designYourMultipleLabelsWithStartingFrame:CGRectMake(588,200,200,28)]; // pass the frame of the first label.
现在在您的视图 class 中添加多个 UILabel
,假设您已设置 labelArrays
.
// code in view class
-(void) designYourMultipleLabelsWithStartingFrame:(CGRect) frame{
float xCoordinate = frame.origin.x;
float yCoordinate = frame.origin.y;
for(int counter = 0; counter < [labelArrays count]; counter++){
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(xCoordinate,yCoordinate,labelWidth,labelHeight)];
label.text = [labelArrays objectAtIndex:counter];
[self addSubview:label];
// update the x coordinate or y coordinate according to your design. Let's say we need the labels in vertical order, so update the y Coordinate by labelHeight and gap between two labels.
yCoordinate = yCoordinate + labelHeight + gapBetweenTwoLabels.
}
}
希望这会有所帮助。
我有一个可变数组如下
labelArrays = [NSMutableArray arrayWithObjects:@"One”, @“Two”, @“Three", nil];
我想将所有列表数组值显示为单独的标签。这就像使用数组值动态显示 UILabel
一样。我所做的如下,
int count=20;
for(int i = 0; i < [labelArrays count]; i++){
label1 = [[UILabel alloc]init];
label1.text = [labelArrays componentsJoinedByString:@" "];
label1.textColor = [UIColor colorWithRed:0.643 green:0.643 blue:0.639 alpha:1];
label1.layer.borderWidth = 1.0;
label1.layer.cornerRadius = 8;
count+=20;
[self addSubview:label1];
}
这里我只能显示单个标签内的所有数组值。如何动态显示多个标签中的数组值。
//viewcontroller
myView.label1.frame =CGRectMake(588,200,200,28);
只是一个想法 - 使用 tableview 来显示数组不是更好吗?
看起来您可能只需要处理它们在视图中的位置,因为代码看起来是在添加标签,但实际上是将它们放在彼此之上。
你的编码没问题,但你没有添加框架,试试这个
label1 = [[UILabel alloc] initWithFrame: CGRectMake(0,count,aslikeyourwidth,aslikeyourheight)];
为 view
定义一个 public 方法,您愿意在其中添加 UILabel
,将 CGRect
传递给该方法,然后根据您的设计,每次迭代更新 x 坐标或 y 坐标。按照以下步骤操作:
// code in your viewcontroller
[myView designYourMultipleLabelsWithStartingFrame:CGRectMake(588,200,200,28)]; // pass the frame of the first label.
现在在您的视图 class 中添加多个 UILabel
,假设您已设置 labelArrays
.
// code in view class
-(void) designYourMultipleLabelsWithStartingFrame:(CGRect) frame{
float xCoordinate = frame.origin.x;
float yCoordinate = frame.origin.y;
for(int counter = 0; counter < [labelArrays count]; counter++){
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(xCoordinate,yCoordinate,labelWidth,labelHeight)];
label.text = [labelArrays objectAtIndex:counter];
[self addSubview:label];
// update the x coordinate or y coordinate according to your design. Let's say we need the labels in vertical order, so update the y Coordinate by labelHeight and gap between two labels.
yCoordinate = yCoordinate + labelHeight + gapBetweenTwoLabels.
}
}
希望这会有所帮助。