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

Add Number in Parse.com

Hi Guys, So Stone Preston was kind enough to help me with my previous question.
I'm using a tableview.

I now need to be able to add 1 to a column in parse.com called "likedKey". I then need this number to be able to save and reload into a UILabel. So far, I have a UILabel set up and in IBAction setup to handle this. I'm just not sure how to add that number (and save it to the appropriate cell in that row in Parse).

Any ideas?

Thanks!!!!

1 Answer

In Parse you can use increment method for adding numbers.

  PFObject * post = [PFObject objectWithoutDataWithClassName:@"Post" objectId:@"<Post Object ID>"];

  [post incrementKey:@"commentCount"];

  // Save Post in DB
  [post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

    // Get the Lastest Comment Count

    [post refreshInBackgroundWithBlock:^(PFObject *object, NSError *error) {
      NSLog(@"Comment Count %@", object[@"commentCount"]);
    }];
  }];

Hi Camilo, Thanks, this is working...however, I don't want to be restricted to a single objectId. Is there any way I can use this for all objectIds?

You need to create a new Column that will store the "Like Count" in DB.

Here's the API Doc for Increment Method

http://www.parse.com/docs/ios/api/Classes/PFObject.html#//api/name/incrementKey:

When you Select a Cell in didSelectRowAtIndexPath method

  • (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

You Increment the counter and Update the Datasource for Cell TextLabel.

Then you use for showing the new value

[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];

Hope it helps :)

Ok, so I have that column called LikedKey. I really want to thank you for all your help so far. :)

I'm not sure if you say this (because I updated my question), but how do I load all object ides, not just the one I hard coded in?

Maybe this is what you are looking for

//
//  JMPTLikeKeysTableViewController.m
//
//  Created by Camilo Castro on 23-05-14.
//

#import "JMPTLikeKeysTableViewController.h"
#import <Parse/Parse.h>

@interface JMPTLikeKeysTableViewController ()
@property (nonatomic) NSMutableArray * likeKeys;
@end

@implementation JMPTLikeKeysTableViewController

- (NSMutableArray *) likeKeys {
  if (!_likeKeys) {
    _likeKeys = [NSMutableArray array];
  }
  return _likeKeys;
}

- (void)viewDidLoad
{
  PFQuery * query = [PFQuery queryWithClassName:@"Likes"];

  [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    self.likeKeys = [objects mutableCopy];

    [self.tableView reloadData];
  }];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.likeKeys.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString * CellIdentifier = @"LikeKey";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    PFObject * likeKey = self.likeKeys[indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@", likeKey[@"likeKey"]];

    // Configure the cell...

    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  PFObject * likeKey = self.likeKeys[indexPath.row];

  // Increment the counter when the cell is selected

  [likeKey incrementKey:@"likeKey"];

  [likeKey saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (succeeded) {
      // Get the Lastest Counter Value
      [likeKey refreshInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        // Update The Cells
        self.likeKeys[indexPath.row] = object;

        [tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
      }];
    }
  }];
}

@end