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
Jared Gross
2,686 PointsRetrieving arrays
I am trying to modify the Ribbit project for learning purposes. What I have is a key on Parse: @"titleLabel" and a pickerView on my view controller. I would like to populate the picker with a list of all the files on Parse with keys matching :@"titleLabel". Below is my code for populating the picker but it doesn't work and my app crashes. Having trouble figuring out why.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
PFQuery *query = [PFQuery queryWithClassName:@"Images"];
[query whereKey:@"titleLabel" equalTo:[[PFUser currentUser] objectId]];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
// We found messages!
[self.textArray addObjectsFromArray:objects];
[self.pickerView reloadAllComponents];
NSLog(@"Retrieved %d Messages", [self.textArray count]);
}
}];
if ([self.textArray count] == 0)
[self.textArray addObject:@"No messages to display"];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.textArray objectAtIndex:row];
}
2 Answers
Stone Preston
42,016 Points[query whereKey:@"titleLabel" equalTo:[[PFUser currentUser] objectId]];
is the titleLabel actually equal to the current user id in any case? I would think titleLabel would be a title of some sort? Not sure what your data really is. However if that is the case then the array would be populated with your no messages to display string.
do you have the following methods in your code as well?
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return #length of your array;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
you already have this method in your code
}
you may also have to implement a delegate method
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
Stone Preston
42,016 Pointsok it looks to me like your query is wrong. your current query returns 0 because there arent any images with a title equal to an object ID (i assume)
[query whereKey:@"titleLabel" equalTo:[[PFUser currentUser] objectId]];
you probably dont have any titleLabels that are equal to the current user objectIds. Im assuming they are titles of some sort. Are you wanting to retrieve the images for a specific person? If so it should be something like
[query whereKey:@"recipient" equalTo:[[PFUser currentUser] objectId]];
you would have to have defined recipient as a key beforehand. That query will return an array of image objects, which you can then access the titleLabel attributes using dot notation.
Jared Gross
2,686 PointsYep, right again. Ha I had actually been reworking it and come up with this solution to my problem but things still aren't working correctly:
// set up the data in the view controller
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
PFQuery *query = [PFQuery queryWithClassName:@"Images"];
[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
// found messages!
self.textArray = objects;
NSLog(@"Retrieved %d images", [self.textArray count]);
}
}];
// sets up the pickerView with number of components to display
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// sets up the number of rows for the pickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
return [self.textArray count];
}
// sets up the pickerView with titles from Parse
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
PFObject *title = [self.textArray objectAtIndex:row];
self.textArray = [title objectForKey:@"titleLabel"];
return [self.textArray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row forComponent: (NSInteger)component {
NSLog(@"You selected this: %@", [self.textArray objectAtIndex:row]);
}
So now when I run it, my NSLog returns the correct number of images associated with the user. My problem, however, is still that I need to get the @"titleLabel" key for each image and string it to the pickerView through my textArray... Which is what I have tried to do above in my titleForRow: method.
Stone Preston
42,016 Pointshmm i think you are on the right track.
try
PFObject *image = [self.textArray objectAtIndex:row];
return [image objectForKey:@"titleLabel"];
Jared Gross
2,686 Pointsokay. I did. everything still runs fine and is still logging the right number of images from the backend but the picker is still not being populated (I see how your code would work a lot better btw) unfortunately I'm still stuck in the mud haha
Stone Preston
42,016 Pointstry logging the string that is being returned just to see what it says
NSLog("%@", [image objectForKey@"titleLabel"]);
Jared Gross
2,686 PointsGot it! I had forgot to call reloadComponents on the pickerView the second time around. Thanks for your help! Especially that last hint about the PFObject!
Stone Preston
42,016 Pointsawesome glad you figured it out
Jared Gross
2,686 PointsJared Gross
2,686 PointsYes, on Parse the key is set to a string called titleLabel and it all shows up on the backend correctly. And you're correct, it does display my "no messages to display" string but for some reason isn't reading the messages from the backend. I'd already implemented all of the methods you listed but my NSLog always returns "1" and it's the "no messages to display" string. I'm stumped.