Josue Espinosa
Josue Espinosa

Reputation: 5089

Programmatically create text fields?

I have an application with objects that have relationships. A "Person" object can have multiple "evaluation" objects. It starts with a navigation controller. Tapping a Person object from a table view takes you to a tableview with all their "evaluations". At the top of all their evaluations, I have 1 static cell that says "All Evaluations" in which I would like to display all their evaluations.

The problem is since I won't know in advance how many evaluations they have, I can't create the right amount of textfields to display all the evaluations.

Let's say this particular person has 7 Evaluations and all evaluation objects have just 1 attribute, "time". How could I dynamically create 7 text fields with the correlating 7 Evaluation objects displayed in them?

Sorry if I was unclear. I have tried creating a UIView in IB and then creating that same view multiple times for each evaluation but failed miserably. Can anyone push me in the right direction please?

Upvotes: 0

Views: 336

Answers (1)

ninja_iOS
ninja_iOS

Reputation: 1223

If you are using IB you are reusing the same view over and over. Create new UILabel with it:

float currentY = 0.0f;
float spacing =5.0f;

for(Evaluation *evaluation in EvalationsArray)
{
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(yourX,currentY,yourWidht, yourHeight)];
    [textLabel setText:[NSString stringWithFormat:@"%@ : %@",evaluation.name,evaluation.time]];
    [self.view addSubview:textLabel]
    currentY = currentY+spacing+yourHeight;
}

Upvotes: 2

Related Questions