1

I'm trying to insert a UILabel into an array and then display it in a certain point on the screen. Using a for loop, I'm counting the amount of elements in a array, and for every element, I want to create a label. This is only producing one label at the moment (the first element in the array).

 int y = 260;

for(int i = 0; i < _jsonArray.count; i ++)
{
    NSNumber *one = [[_jsonArray objectAtIndex:i] objectForKey:@"amount"];
    NSString *name = [[_jsonArray objectAtIndex:i] objectForKey:@"name"];


    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, y, 200, 40)];
    [myLabel setBackgroundColor:[UIColor clearColor]];
    [myLabel setText:name];
    [_slices addObject:one];
    [[self view] addSubview:myLabel];
    y=y+20;

}

Any help would be much appreciated!

Paulw11
  • 108,386
  • 14
  • 159
  • 186
spogebob92
  • 1,474
  • 4
  • 23
  • 32
  • Have you used the debugger to single-step through your code and verified that there is more than 1 element in the array? – Paulw11 Apr 03 '14 at 00:14
  • Yeah I have a pie chart (XYPieChart) that is displaying all the elements from the array – spogebob92 Apr 03 '14 at 00:14
  • Well your code looks correct, so my next guess would be when you add the `UILabel` to the view it is either falling outside of the view's bounds or being obscured by something else. I presume this is just 'test' code, as a `UITableView` would be a better solution – Paulw11 Apr 03 '14 at 00:19
  • what is the result already of this code? – hcarrasko Apr 03 '14 at 00:20
  • incidentally, you aren't actually storing the `UILabel` in an array anywhere... – Paulw11 Apr 03 '14 at 00:20
  • I suspect you're stacking the labels on top of each other, possibly due to auto-layout. – mharper Apr 03 '14 at 00:21
  • I think I see where this is going wrong. Each label created is called "myLabel". Therefore, when I set the text of "myLabel" it is going to change all the labels. Any idea how to get around this? – spogebob92 Apr 03 '14 at 00:21
  • Any idea how to solve auto layout? – spogebob92 Apr 03 '14 at 00:22
  • Maybe you can try using tags based on the index of the array – klcjr89 Apr 03 '14 at 00:22
  • 2
    @SamStone myLabel is scoped inside of the loop, so it's a "new" myLabel every time through. The instance will be retained by the view you're adding it to. So, no, you're not setting the text of the same label each time. – mharper Apr 03 '14 at 00:23
  • Ah, got you mharper. Maybe it is the auto layout thing then, but I'm not sure how when I add 20 to 'y' after each loop. – spogebob92 Apr 03 '14 at 00:24
  • 1
    This answer may help: http://stackoverflow.com/a/11730666/128317 – mharper Apr 03 '14 at 00:24
  • Thank you very much mharper. Worked a charm. If you want to post an answer I'll mark you correct? – spogebob92 Apr 03 '14 at 00:27
  • As an aside, you are apparently creating a label with a height of 40, and incrementing the Y coordinate for each subsequent label by only 20. Consequently, even when this does work, the labels will overlap each other. – mharper Apr 03 '14 at 00:28
  • I can't take credit for simply pointing you to someone else's answer. Glad you're unstuck! – mharper Apr 03 '14 at 00:29
  • Thanks. I was going to fiddle with those values once I had this bit nailed. – spogebob92 Apr 03 '14 at 00:29
  • use IBOutletCollection to get array of labels. – Vineesh TP Apr 03 '14 at 04:26

1 Answers1

1

Make arrayLabels a property so you can access for anywhere in your view controller

  _arrayLabels = [NSMutableArray array];
  int y = 260;

  for(int i = 0; i < _jsonArray.count; i ++)
  {
    NSNumber *one = [[_jsonArray objectAtIndex:i] objectForKey:@"amount"];
    NSString *name = [[_jsonArray objectAtIndex:i] objectForKey:@"name"];


    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, y, 200, 40)];
    [myLabel setBackgroundColor:[UIColor clearColor]];
    [myLabel setText:name];
    [_slices addObject:one];
    [[self view] addSubview:myLabel];
    [_arrayLabels addObject:myLabel];
    y=y+20;
  }

then you can obtain the label with (UILabel *)[_arrayLabels objectAtIndex:i];

aguilarpgc
  • 1,181
  • 12
  • 24
  • While your answer addresses the question in the title, it doesn't address the problem stated in the body - only one label is visible – Paulw11 Apr 03 '14 at 00:26
  • Hi. This would actually help me, but I'm getting errors "Use of undeclared identifier". Any ideas? EDIT. Solved that error (create array in header file). But now I get an error on the "addObject" line. Here is what I put in my header file: @property (retain, nonatomic) IBOutletCollection(UILabel) NSArray *arrayLabels; – spogebob92 Apr 03 '14 at 00:35
  • Make sure that in your .h file add `@property NSMutableArray *arrayLabels;` – aguilarpgc Apr 03 '14 at 00:38
  • Declare it as an `NSMutableArray` – aguilarpgc Apr 03 '14 at 00:39
  • This may be a cheeky, but could you explain why the "(retain, nonatomic)" was not needed here? Thank you btw! – spogebob92 Apr 03 '14 at 00:40
  • I put that as fast as i could, did you try with `@property (retain, nonatomic) IBOutletCollection(UILabel) NSMutableArray *arrayLabels;` – aguilarpgc Apr 03 '14 at 00:42