0

我想在我的数组中添加与对象一样多的标签。但是如何对齐它们??我想要一个在另一个之下,并且页面完成后它应该从下一列开始。但是我尝试使用代码覆盖另一个。

- (void)viewDidLoad
{
    [super viewDidLoad];
    array = [[ NSMutableArray alloc]initWithObjects:@"aaa",@"bbb",@"ccc",@"ddd",nil];
    for( int i=0;i<[array count];i++)
    {
        NSString *theText = [array lastObject];
        UILabel *label = [[UILabel alloc]init];
        label.text = theText;
        label.backgroundColor = [UIColor clearColor];
        label.lineBreakMode = UILineBreakModeWordWrap;
        label.frame = CGRectMake(0, label.frame.origin.y + label.frame.size.height, size.width + 20, size.height + 20);
        [self.view addSubview:label];
    }
}
4

5 回答 5

5

试试这个代码..

array = [[ NSMutableArray alloc]initWithObjects:@"aaa",@"bbb",@"ccc",@"ddd",nil];
UILabel *label;
int y =10;
for( int i=0;i<[array count];i++)
{
    NSString *theText = [array objectAtIndex:i];
    label = [[UILabel alloc]init];
    label.text = theText;
    label.backgroundColor = [UIColor clearColor];
   label.frame = CGRectMake(0, y + label.frame.size.height, size.width + 20,   size.height + 20);
[self.view addSubview:label];
    y = y +label.frame.size.height+5;
}
于 2012-06-26T11:25:20.103 回答
0

你总是在循环中取最后一个对象。相反,您应该从正确的索引中获取对象。

int y = 10;
for( int i=0;i<[array count];i++)
{
    //NSString *theText = [array lastObject]; Wrong One
    NSString *theText = [array objectAtIndex:i]; //Correct One
    ...
    ...
    label.frame = CGRectMake(0, y, size.width + 20,   size.height);
    y += size.height + 5;

}

此外,您需要正确管理 origin.y。

于 2012-06-26T11:25:21.523 回答
0
- (void)viewDidLoad
{
    float originFromY = 0;

    [super viewDidLoad];
    array = [[ NSMutableArray alloc]initWithObjects:@"aaa",@"bbb",@"ccc",@"ddd",nil];
    for( int i=0;i<[array count];i++)
    {
         NSString *theText = [array lastObject];
         UILabel *label = [[UILabel alloc]init];
         label.text = theText;
          label.backgroundColor = [UIColor clearColor];
         lineBreakMode:UILineBreakModeWordWrap];
        label.frame = CGRectMake(0, originFromY, 50,40);
       [self.view addSubview:label];
       originFromY += 60;
    }
 }

这可能会帮助你

于 2012-06-26T11:26:18.790 回答
0

您修改后的代码

- (void)viewDidLoad
{
[super viewDidLoad];
array = [[ NSMutableArray alloc]initWithObjects:@"aaa",@"bbb",@"ccc",@"ddd",nil];
int y = 0;
for( int i=0;i<[array count];i++)
{
    NSString *theText = [array lastObject];
    UILabel *label = [[UILabel alloc]init];
    label.text = theText;
    label.backgroundColor = [UIColor clearColor];
    lineBreakMode:UILineBreakModeWordWrap];
    label.frame = CGRectMake(0, y, size.width + 20, size.height + 20);
    y += size.height + 20;
    [self.view addSubview:label];
}
}
于 2012-06-26T11:26:25.547 回答
0

做这个:

- (void)viewDidLoad
{
  [super viewDidLoad];
  array = [[ NSMutableArray alloc]initWithObjects:@"aaa",@"bbb",@"ccc",@"ddd",nil];
  for( int i=0;i<[array count];i++)
  {
    UILabel *label = [[UILabel alloc]init];
    label.text = [array objectAtIndex:i];
    label.backgroundColor = [UIColor clearColor];

    label.frame = CGRectMake(10, (i*30)+10, 100, 30); // here height is 30 and width is 100;
    [self.view addSubview:label];
   }
  }
于 2012-06-26T11:28:00.570 回答