سؤال

How do i display all data in a UILabel?

I am currently using this :

    for (NSManagedObject *obj in matchingData)
    {
        lastname = [obj valueForKey:@"lastname"];
        name = [obj valueForKey:@"name"];

        NSLog(@"Name:%@\n Last Name %@\n", [obj valueForKey:@"name"],[obj valueForKey:@"lastname"]);
    }

    self.displayLabel2.text = [NSString stringWithFormat:@"Guest : %@  %@" ,name, lastname];
}

In the console I get all my data but in UILabel (displayLabel2) I just get one name…i just don't get it…

هل كانت مفيدة؟

المحلول

The way that your code reads, on each loop through the obj NSManagedObject, the name and lastname will be reset. So the label will only display the final loop's data.

try creating a string within the loop

NSMutableString *stringText = [NSMutableString string];

for (NSManagedObject *obj in matchingData)

{
    lastname = [obj valueForKey:@"lastname"];
    name = [obj valueForKey:@"name"];

    NSLog(@"Name:%@\n Last Name %@\n", [obj valueForKey:@"name"],[obj valueForKey:@"lastname"]);
    stringText = [NSString stringWithFormat:@"%@ %@", stringText, [NSString stringWithFormat:@"Guest : %@  %@" ,name, lastname]];

}

self.displayLabel2.text = stringText;

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top