How to create NSMutableArray of UILabels?

爷,独闯天下 提交于 2019-12-08 10:00:37

问题


I have a problem here. I'm creating UILabels dynamically to be displayed into a UIView. Here is the code:

for (flower in flowerList)
{               

    // Create the Qty label
    CGRect rectQty = CGRectMake(x , y, 25, labelHeight);
    UILabel *flowerQuantityLabel = [[UILabel alloc] initWithFrame:rectQty];
    NSString *fQty = [[NSString alloc] initWithFormat:@"%d", flower.flowerQty];
    [flowerQuantityLabel setText:fQty];
    // Loading refrence into a array  
        // here is the problem 
    [flowersQtyLabels addObject:flowerQuantityLabel];

    // Create the name label
    CGRect rectName = CGRectMake((x+offset) , y, labelWidth, labelHeight);
    UILabel *flowerNameLabel = [[UILabel alloc] initWithFrame:rectName];
    [flowerNameLabel setText:flower.flowerName];

    y = y + 40.0;
    [self.view addSubview:flowerQuantityLabel];
    [self.view addSubview:flowerNameLabel];

}
  • The number of elements in the array changes every time.
  • I need to recalculate "flower.flowerQty" and update the result into the label (flowerQuantityLabel).
  • I tried to load a reference to each flowerQuantityLabel into a NSMuttableArray (flowersQtyLabels), in order to change its content with the result of the recalculation.
  • At the end of the for, the array flowersQtyLabels is empty. I do not know what the problem is.

I hope some one can help me.


回答1:


I had the NSMuttableArray not initialized properly.



来源:https://stackoverflow.com/questions/1013325/how-to-create-nsmutablearray-of-uilabels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!