本文介绍了如何创建UILabels的NSMutableArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有问题。我正在动态创建UILabels以显示在UIView中。下面是代码:
for(flower in flowerList)
{
创建数量标签
CGRect rectQty = CGRectMake(x,y,25,labelHeight);
UILabel * flowerQuantityLabel = [[UILabel alloc] initWithFrame:rectQty];
NSString * fQty = [[NSString alloc] initWithFormat:@%d,flower.flowerQty];
[flowerQuantityLabel setText:fQty];
//将refrence载入数组
//这里是问题
[flowersQtyLabels addObject:flowerQuantityLabel];
//创建名称标签
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];
}
- 元素数
- 我需要重新计算flower.flowerQty,并将结果更新为标签(flowerQuantityLabel)。
- 以将每个flowerQuantityLabel的引用加载到NSMuttableArray(flowersQtyLabels)中,以便使用重新计算的结果更改其内容。
- 在for结束时,数组flowersQtyLabels为空。我不知道是什么问题。
我希望有人能帮助我。
解决方案
我的错误,我的NSMuttableArray未正确初始化。
b $ b
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.
解决方案
My mistake, I had the NSMuttableArray not initialized properly.
Thanks!!!
这篇关于如何创建UILabels的NSMutableArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!