Objective-C for each item in NSArray create UI Label - ios

I have this block of code here:
for (id object in _theParamenterArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 200)];
label.text = #"Custom Label";
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
}
What I am trying to do is create a label for each item in NSArray. My array has 3 rows and each row as the following values: Assemble, Division, Project and WorkOrder and I want to put each value in a UILabel. how would I accomplish this?
I have tried the following:
int counter = 0;
for (id object in _theParamenterArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Assemble"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(110, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Division"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelTwo];
UILabel *labelThree = [[UILabel alloc]initWithFrame:CGRectMake(210, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Project"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelThree];
UILabel *labelFour = [[UILabel alloc]initWithFrame:CGRectMake(310, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"WorkOrder"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelFour];
counter++;
}
but it only displays the WorkOrder for all three rows, I am trying to get all values from each row.
Here is a screenshot of my array:

My issue is when I run this code, I only get 1 label that says "Custom Label"
You do get four labels that look like one because they sit on top of each other, and all say the same thing (i.e. "Custom Label").
In order to fix this, you need to do two things:
Make sure that you give your labels different frames - make a counter, decide how far apart the labels should be, and adjust coordinates accordingly, and
Set different text into the labels - for example, use label.text = object (assuming that object is NSString)
The way you adjust the origin of the frame depends on where you want your labels to be in relation to each other. To stack them vertically, use this code:
int counter = 0;
for (id object in _theParamenterArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Assemble"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(110, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Division"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelTwo];
UILabel *labelThree = [[UILabel alloc]initWithFrame:CGRectMake(210, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Project"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelThree];
UILabel *labelFour = [[UILabel alloc]initWithFrame:CGRectMake(310, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"WorkOrder"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelFour];
counter++;
}
The 10+30*counter expression places your labels at positions
(10, 10) (110, 10) (210, 10) (310, 10)
(10, 40) (110, 40) (210, 40) (310, 40)
(10, 70) (110, 70) (210, 70) (310, 70)
(10, 100) (110, 100) (210, 100) (310, 100)

You need to increment the Y value like below (Or set the CGRect to be where you want the labels)
NSArray *obj = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", nil];
int x = 200;
for (NSString *object in obj) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, x, 200, 20)];
x+= 40;
label.text = object;
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
}

Related

Objective-C trying to create a label for each item in NSArray - Not working

I have an array that looks like the image attached
and what I am trying to do is the following:
Run a foreach for this NSArray
Create a label for each item in the array (so 3 Division Labels, 3 Project Labels, 3 WorkOrder Labels and 3 Assemble Labels)
I have the following code:
int counter = 0;
for (id object in _theParamenterArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Assemble"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(110, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Division"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelTwo];
UILabel *labelThree = [[UILabel alloc]initWithFrame:CGRectMake(210, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Project"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelThree];
UILabel *labelFour = [[UILabel alloc]initWithFrame:CGRectMake(310, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"WorkOrder"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelFour];
counter++;
}
My issue with this code is that it only creates 4 labels in total and the data is the last item in the array, so it appears its overriding the values. My question how do I fix this? PLEASE HELP!
you set text only for "label" variable :) It's a Copy/Paste bug :)
rename label -> labelTwo, labelThree, labelFour for lines:
label.text = [object objectForKey:#"WorkOrder"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
Looks like your issue is assigning text and font to label 4 times, try this:
int counter = 0;
for (id object in _theParamenterArray) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30*counter + 10, 200, 200)];
label.text = [object objectForKey:#"Assemble"];
[label setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:label];
UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(110, 30*counter + 10, 200, 200)];
labelTwo.text = [object objectForKey:#"Division"];
[labelTwo setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelTwo];
UILabel *labelThree = [[UILabel alloc]initWithFrame:CGRectMake(210, 30*counter + 10, 200, 200)];
labelThree.text = [object objectForKey:#"Project"];
[labelThree setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelThree];
UILabel *labelFour = [[UILabel alloc]initWithFrame:CGRectMake(310, 30*counter + 10, 200, 200)];
labelFour.text = [object objectForKey:#"WorkOrder"];
[labelFour setFont:[UIFont boldSystemFontOfSize:16]];
[self.view addSubview:labelFour];
counter++;
}
It would help to see the code for the array.

Set name to UILabel as label1, label2 and so on

I want to set name to UILabel as label1,label2,label3 and so on. For that i don't want to write code for label1,label2,... Now i am using this method.
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
.....
But it's not a good method. I only want to write a single line of code for all labels.
How can i implement this? Is it possible to use %d along with label.
Please help.
just set tag of label and get data by using tag
for (int i=0; i<4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
label.tag = i;
[self.view addSubview:label];
}
You can use tag of Label to differentiate from other instance. there is no such logic you are looking and it is not suggestible to differentiate objects.
for (int i = 0; i < 4; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 320, 420)];
label.tag = i;
[self.view addSubview:label];
}
if ([self.view viewWithTag:i]) {
[(UILabel *)[self.view viewWithTag:i] setText:#"Update Text"];
}

Avoid my section header to be redraw in viewForHeaderInSection

I have a UITableView with only 1 section title that contains labels when i reload data some of the label of the section may be hidden. But when I do reload data they are redraw in section I just wanted to know if there is some sort of dequeueReusableCellWithIdentifier: to avoid this like for the cell but for secions.
Here is the code
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 36)];
/* Create custom view to display section header... */
UILabel *labelSeq = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 30, 18)];
[labelSeq setFont:[UIFont boldSystemFontOfSize:12]];
labelSeq.text = #"SEQ";
UILabel *labelSup = [[UILabel alloc] initWithFrame:CGRectMake(50, 2, 330, 18)];
[labelSup setFont:[UIFont boldSystemFontOfSize:12]];
labelSup.text = #"SUPPLIER";
UILabel *labelMod = [[UILabel alloc] initWithFrame:CGRectMake(390, 2, 150, 18)];
[labelMod setFont:[UIFont boldSystemFontOfSize:12]];
labelMod.text = #"MODEL";
UILabel *labelFinish = [[UILabel alloc] initWithFrame:CGRectMake(550, 2, 150, 18)];
[labelFinish setFont:[UIFont boldSystemFontOfSize:12]];
labelFinish.text = #"FINISH";
UILabel *labelQty = [[UILabel alloc] initWithFrame:CGRectMake(710, 2, 30, 18)];
[labelQty setFont:[UIFont boldSystemFontOfSize:12]];
labelQty.text = #"QTY";
UILabel *labelSale = [[UILabel alloc] initWithFrame:CGRectMake(750, 2, 125, 18)];
[labelSale setFont:[UIFont boldSystemFontOfSize:12]];
labelSale.textAlignment = UITextAlignmentRight;
labelSale.text = #"SALE PRICE";
labelSale.tag = 100;
UILabel *labelCost = [[UILabel alloc] initWithFrame:CGRectMake(885, 2, 125, 18)];
[labelCost setFont:[UIFont boldSystemFontOfSize:12]];
labelCost.textAlignment = UITextAlignmentRight;
labelCost.text = #"COST";
labelCost.tag = 200;
UILabel *labelDesc = [[UILabel alloc] initWithFrame:CGRectMake(50, 17, 700, 18)];
[labelDesc setFont:[UIFont boldSystemFontOfSize:12]];
labelDesc.text = #"DESCRIPTION";
UILabel *labelUnit = [[UILabel alloc] initWithFrame:CGRectMake(750, 17, 125, 18)];
[labelUnit setFont:[UIFont boldSystemFontOfSize:12]];
labelUnit.textAlignment = UITextAlignmentRight;
labelUnit.text = #"UNIT PRICE";
labelUnit.tag = 300;
UILabel *labelProfit = [[UILabel alloc] initWithFrame:CGRectMake(885, 17, 125, 18)];
[labelProfit setFont:[UIFont boldSystemFontOfSize:12]];
labelProfit.textAlignment = UITextAlignmentRight;
labelProfit.text = #"PROFIT";
labelUnit.tag = 400;
[view addSubview:labelSeq];
[view addSubview:labelSup];
[view addSubview:labelMod];
[view addSubview:labelFinish];
[view addSubview:labelQty];
[view addSubview:labelSale];
[view addSubview:labelCost];
[view addSubview:labelDesc];
[view addSubview:labelUnit];
[view addSubview:labelProfit];
[view setBackgroundColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]]; //your background color...
_headerView = view;
return view;
}
Add a property to your class to hold on to that UIView. Then in -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section, just check to see if that property is nil or not. If it's nil, create it and set that property to it, otherwise just use the UIView saved in the property. If it requires updating it some, then you can do that at that point. It looks like all your content is static, so you should never need to update the view at all after the first time you create it.

Why is nothing displaying in my UIView?

I have following code that adds a UIView and then adds a UIImageView and UILabel, the UIView display correctly but nothing i have put in side the view is showing. I have looked everywhere but i cannot find an answer. The UIView is presented under certain conditions and when it is tapped it dissapears.
Here is my code.
UIView *actionView = [[UIView alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[actionView addGestureRecognizer:singleFingerTap];
actionView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
actionView.layer.cornerRadius = 5;
actionView.layer.masksToBounds = YES;
[self.view addSubview:actionView];
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
actionText.Frame = CGRectMake(20, 180, 280, 165);
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109, 273, 40, 40);
[actionView addSubview:blockView];
Change your frame positions like..
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 40)];
actionText.text = #"Hello";
actionText.textColor = [UIColor redColor];
actionText.textAlignment = NSTextAlignmentCenter;
[actionText setTextColor:[UIColor redColor]];
[actionText setBackgroundColor:[UIColor clearColor]];
[actionText setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[actionView addSubview:actionText];
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"trash-icon.png"]];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(109,70, 40, 40);
[actionView addSubview:blockView];
When you set the frame of a view, it is important to remember that the origin relates to the coordinate system of the superview it is added to.
So when you set the frame of your label to:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 280, 165)];
the origin of x = 20, y = 180 end up being the origin WITHIN the actionView. Now this is your problem because your actionView is only 165px tall, and you are telling the actionText subView that it's y origin is 180, which is well outside the bottom of the actionView.
for your actionText, you should allocate it as follows:
UILabel *actionText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 165)];
and for your blockView, something like:
blockView.frame = CGRectMake(89,93, 40, 40);

iOS create UILabels dynamically

Sometimes I want my view to contain 5 UILabels, sometimes 3 and sometimes n.
The number of UILabels depends on data that's fetched from a website.
You'll have to make them in code instead of interface builder
for (int i = 0; i < n; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
label.text = #"text"; //etc...
[self.view addSubview:label];
[label release];
}
A generic answer for a generic question:
while (labelsToDisplay)
{
UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
[label setText:#"someText"];
[aViewContainer addSubview:label];
[label release];
}
NSArray *dataArray;
float xCoordinate=10.0,yCoordinate=10.0,width=100,height=40;
float ver_space=20.0;
for (int i = 0; i <dataArray.count; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(xCoordinate,yCoordinate,width,height)];
label.text = [dataArray objectAtIndex:i];
[self.view addSubview:label];
yCoordinate=yCoordinate+height+ver_space;
}
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(125, 12,170,20)];
lbl.text=#"IOS";
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:#"AlNile" size:10.0];
lbl.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
lbl.layer.borderColor=[UIColor blackColor].CGColor;
lbl.layer.borderWidth=1.0f;
lbl.layer.cornerRadius = 6.0f;
[self.view addSubview:lbl];
UILabel *lblTitle=[[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
[lblTitle setText:#"MAK"];
[lblTitle setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:lblTitle];
-Here UILable will be created dynamically.
-but property will be set differently.
Create a TextView to show the text on the labels and a NSArray to contain the data.
For more information:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html
http://developer.apple.com/library/ios/documentation/uikit/reference/UITextView_Class/Reference/UITextView.html

Resources