Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从NSMutableArray动态显示UILabel_Ios_Objective C_Ios8 - Fatal编程技术网

Ios 从NSMutableArray动态显示UILabel

Ios 从NSMutableArray动态显示UILabel,ios,objective-c,ios8,Ios,Objective C,Ios8,我有一个可变数组,如下所示 labelArrays = [NSMutableArray arrayWithObjects:@"One”, @“Two”, @“Three", nil]; 我想将所有列表数组值显示为单个标签。这类似于使用数组值动态显示UILabel。我所做的如下: int count=20; for(int i = 0; i < [labelArrays count]; i++){ label1 = [[UILabel alloc]init]; label1.te

我有一个可变数组,如下所示

 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];
}

只是一个想法-使用tableview来显示数组不是更好吗


看起来您可能需要处理它们在视图上的位置,因为代码看起来像是在添加标签,但它将标签放置在彼此的上方。

只是一个想法-使用tableview显示阵列不是更好吗


看起来您可能只需要处理它们在视图上的位置,因为代码看起来像是在添加标签,但它将标签放置在彼此上方。

您的编码很好,但您没有添加框架,请尝试此操作

label1 =  [[UILabel alloc] initWithFrame: CGRectMake(0,count,aslikeyourwidth,aslikeyourheight)];

您的编码很好,但未添加帧,请尝试此操作

label1 =  [[UILabel alloc] initWithFrame: CGRectMake(0,count,aslikeyourwidth,aslikeyourheight)];

视图
定义一个公共方法,在该方法中您愿意添加
UILabel
,向该方法传递一个
CGRect
,然后根据您的设计,在每次迭代中更新x坐标或y坐标。请按照以下步骤操作:

// code in your viewcontroller
[myView designYourMultipleLabelsWithStartingFrame:CGRectMake(588,200,200,28)]; // pass the frame of the first label.
现在在视图类中添加多个
UILabel
,假设您已经设置了
labelarray

// 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.
   }
}
//视图类中的代码
-(无效)使用启动框架(CGRect)框架设计您的多行标签{
float xCoordinate=frame.origin.x;
float yCoordinate=frame.origin.y;
用于(int计数器=0;计数器<[标签阵列计数];计数器++){
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(xCoordinate,ycCoordinate,labelWidth,labelHeight)];
label.text=[LabelArray对象索引:计数器];
[自添加子视图:标签];
//根据您的设计更新x坐标或y坐标。假设我们需要垂直顺序的标签,因此通过标签高度和两个标签之间的间隙更新y坐标。
yCoordinate=yCoordinate+标签高度+两个标签之间的间隙。
}
}

希望这会有所帮助。

视图定义一个公共方法,在该方法中您愿意添加
UILabel
,将
CGRect
传递给该方法,然后根据您的设计,在每次迭代中更新x坐标或y坐标。请按照以下步骤操作:

// code in your viewcontroller
[myView designYourMultipleLabelsWithStartingFrame:CGRectMake(588,200,200,28)]; // pass the frame of the first label.
现在在视图类中添加多个
UILabel
,假设您已经设置了
labelarray

// 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.
   }
}
//视图类中的代码
-(无效)使用启动框架(CGRect)框架设计您的多行标签{
float xCoordinate=frame.origin.x;
float yCoordinate=frame.origin.y;
用于(int计数器=0;计数器<[标签阵列计数];计数器++){
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(xCoordinate,ycCoordinate,labelWidth,labelHeight)];
label.text=[LabelArray对象索引:计数器];
[自添加子视图:标签];
//根据您的设计更新x坐标或y坐标。假设我们需要垂直顺序的标签,因此通过标签高度和两个标签之间的间隙更新y坐标。
yCoordinate=yCoordinate+标签高度+两个标签之间的间隙。
}
}


希望这会有所帮助。

您有什么问题?什么不起作用?您没有为
标签1
设置框架。它的框架是什么?创建一个新标签,并在每次迭代中更新标签框架,使用上面的代码,您将在另一个标签上添加多个标签。什么是label1,它是retain属性?删除for循环,并在标签上添加一个框架。@Larme问题是,数组元素没有显示为单独的UILabel。它们合并在一个UILabel中。但我需要的是在单独的标签中显示数组元素。这段代码在视图中,我正在调用另一个控制器中的帧,您有什么问题?什么不起作用?您没有为
标签1
设置框架。它的框架是什么?创建一个新标签,并在每次迭代中更新标签框架,使用上面的代码,您将在另一个标签上添加多个标签。什么是label1,它是retain属性?删除for循环,并在标签上添加一个框架。@Larme问题是,数组元素没有显示为单独的UILabel。它们合并在一个UILabel中。但我需要的是在单独的标签中显示数组元素。这段代码在视图中,我在另一个视图中调用帧controller@sabarish--给出反馈很容易优化答案对不起,忘记提到上面的代码,我在另一个控制器中提到了框架。我可以显示标签,但不能作为单独的标签显示。它已合并到单个标签中并显示所有阵列elements@sabarish--给出反馈很容易优化答案对不起,忘记提到上面的代码,我在另一个控制器中提到了框架。我可以显示标签,但不能作为单独的标签显示。它已合并到一个标签中,并显示所有数组元素。不幸的是,它不会像那样简单,因为为了使标签不相互重叠,它们需要单独的帧。因此,在for循环中,您必须为每个标签添加帧位置。不幸的是,这不会像那样简单,因为为了使标签不相互重叠,每个标签都需要单独的帧。因此,在for循环中,您必须为每个标签添加帧位置。以相同的方式,而不是
UILabel
,如果我添加
UIButton
,我应该如何填充按钮?我尝试使用
setTag
并传递了计数器值,但无效。您必须使用setTitle:forState:of UIButton来设置UIButton的标题。以相同的方式而不是
UILabel
,如果我添加
UIButton
,我应该如何填充按钮?我尝试了
setTag
并传递了计数器值,但没有成功。您必须使用setTitle:forState:of ui按钮来设置