Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Photo Browser iPhone App Connecting to an API using OAuth Caching Photos

Robert Mckay
Robert Mckay
11,089 Points

How to take instgram JSON and use it in UILabel underneath each picture?

I have been able to use the existing photo controller to populate my dictionary with user information to populate the labels however, when I created a new controller to hold the code outside of PhotoController - it doesn't even call the new controllers - additionally, am looking for a way to get this information to post underneath each picture which seems to continue to allude my grasp. I have been able to get static labels in each cell as well a dynamic (used a counting sequence to number the cells, however I have been unable to use the information from the dictionary. I created the following:

blueclienLabelConroller .m/.h The following method:

+ (void)labelForPhoto:(NSDictionary *)labelindex completion:(void(^)
                                                        (UILabel *label))completion;
+ (void)labelForPhoto:(NSDictionary *)labelindex completion:(void(^)
                                                        (UILabel *label))completion{
    //Prevents crash if some images return as unavaiable

    if (labelindex == nil || completion == nil){

    NSString *key = [[NSString alloc] initWithFormat:@"%@", labelindex[@"id"]];
    UILabel *label = [[SAMCache sharedCache] objectForKey:key];

    if (label){

    NSString *userName = [[NSString alloc] initWithFormat:@"%@", labelindex[@"user"][@"username"]];
    NSData* transformdata = [userName dataUsingEncoding:NSUTF8StringEncoding];
    NSData *nameData = [[NSData alloc] initWithData:transformdata];
    label = [[UILabel alloc] init];
    NSString *usernamestring = [[NSString alloc] initWithData:nameData encoding:NSUTF8StringEncoding];
    label.text = usernamestring;
    [[SAMCache sharedCache] setValue:usernamestring forKey:key];


my attempt to call it from PhotoCell looks like this:

- (void)setNameLabel:(NSDictionary *)labelindex{
    _labelindex = labelindex;
    [blueclientLabelController labelForPhoto:_labelindex completion:^(UILabel *label){

        self.descriptionLabel.text = label;

and photoviewcontroller.m has:

    cell.descriptionLabel = self.labels[indexPath.row];

I hope this is enough info, if I am missing anything let me know! Thank you for taking the time to look. :)

You have a completion handler which is running on a seporate thread, you need to go back to the main thread when setting the textLabel

Example: dispatch_async(dispatch_get_main_queue(), ^{ self.myTextLabel.text = textYouWantToSet; });

Im new too so I hope thats right, lol