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
Gregg Mojica
11,506 PointsPFQuery
Update: likedKey's values are numbers.
Hi Guys, So I'm working on a project.....I need to be able to query Parse.com for a class and find that class's list of values.
I have a column called likedKey and I want to be able to display all the likedKey's values into a label named likedLabel.
So far I've got this much..
PFQuery *query = [PFQuery queryWithClassName:@"myClassName"];
[query whereKey:@"likedKey" ?????];
cell.likedLabel.text = [NSString stringWithFormat:@"%@", query];
But I'm not sure what to do with that second line. Could anyone offer assistance? Stone Preston Thanks!!
1 Answer
Stone Preston
42,016 Pointsyou want to query your backend in viewDidAppear, not in cellForRowAtIndexPath. you want all the objects in the class so you shouldnt need any constraints on your query:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//you dont need to set any contraints on your query, you want all the object in the class. the query below gets all the objects in the class
//if you want more than 100 you need to set a limit on the objects
PFQuery *query = [PFQuery queryWithClassName:@"myClassName"];
[query setLimit: 1000];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
//store the objects in an array property
self.objects = objects;
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
then in cellForRowAtIndexPath set the label value:
//assuming your likeCount is an NSNumber
NSString likeCount = [[[self.objects objectAtIndex:indexPath.row] valueForKey:@"likedKey"] stringValue];
cell.likedLabel.text = likeCount;
Gregg Mojica
11,506 PointsGregg Mojica
11,506 PointsHi Stone, Thanks for the quick reply!
My problem is that when I implement
self.objects = objects; I am given an error: property "objects" not found... is this a property I need to implement in the .h?
Also, what do you mean by "assuming your likeCount" is an NSNumber?
From the code you gave me above, it looks like likeCount is a NSString you declared above? A I mentioned above, I have a column in parse called likedKey that is a number.
I sincerly thank you for your help!
Stone Preston
42,016 PointsStone Preston
42,016 PointsYes you would need to add that property. As For the nsnumber If you look at the method calls you will see I used the stringValue method of NSNumber to convert the value stored in likeKey to a string
Gregg Mojica
11,506 PointsGregg Mojica
11,506 PointsHi Stone! You're amazing! I really appreciate it...everything's working now! :)
Gregg Mojica
11,506 PointsGregg Mojica
11,506 PointsHey Stone, can I pick your brain for one more sec? I want the user to be able to press a button and then add the number 1 to the current likedKey value in Parse. I set up an IBAction for the button and I understand the general gist of how to do it - Add one, save it in parse, then reload the tableview and update the label. But I'm not entirely sure how to add 1 (and save it in parse) each time a user presses that button. Any ideas? Again, thanks a ton for your help!