Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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){
        return;
    }

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

    if (label){
        completion(label);
        return;
    }

    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