iqueqiorio
iqueqiorio

Reputation: 1187

Remove Item from UITableView - getting data from Parse.com

I have a UITableview that loads data from parse.com and displays it. And I want this to be editable so that when a user deletes an items from the table view it deletes it from parse.com

I have used my own tableview and use parseSimpleCell.h, .m for the custom cell

Here are my tableview .h and .m files

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "ParseExampleCell.h"

@interface FavoritesTableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource> {
    NSArray *itemsArray;
}

@property (weak, nonatomic) IBOutlet UITableView *favItemsTable;

@end

Here is the .m file

#import "FavoritesTableViewController.h"

@interface FavoritesTableViewController ()

@end

@implementation FavoritesTableViewController



- (void)viewDidLoad {
    [super viewDidLoad];

    [self performSelector:@selector(retrieveFromParse)];


    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void) retrieveFromParse {

    PFUser *currentUser = [PFUser currentUser];

    PFQuery *query = [PFQuery queryWithClassName:@"UserFavourite"];
    [query whereKey:@"userIdString" equalTo:currentUser.objectId];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            itemsArray = [[NSArray alloc] initWithArray:objects];
        }
        [_favItemsTable reloadData];
    }];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.

    return itemsArray.count;

}

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

    static NSString *CellIdentifier = @"Cell";

    ParseExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    PFObject *tempObject = [itemsArray objectAtIndex:indexPath.row];

    cell.cellTitle.text = [tempObject objectForKey:@"item"];


    cell.tintColor = [UIColor redColor];

    return cell;

}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {


}


@end

I need help with what goes in this method - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

I have looked at other questions and see but the variable objects, that is got front eh retrieveFromParse method isn't available in the commitEditing style method?

I have tried things like this

if (editingStyle == UITableViewCellEditingStyleDelete) {
    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        [self loadObjects];
    }];
}

But is says objects is not found

Thanks for the help in advance

Upvotes: 0

Views: 697

Answers (1)

danh
danh

Reputation: 62676

The thing you tried refers to self.objects, but I don't see anyplace else you use objects.

The way to delete from a table is to delete from the datasource then from the table view. Since you want the object deleted from parse, too, you have an extra step.

// remove from datasource
PFObject *object = itemsArray[indexPath.row];
[itemsArray removeObject:object];

// tell the table to update
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates

// remove from parse
[object deleteInBackground];

Note that this could setup a race condition if you query those same objects right away. If there's risk of that, then use deleteInBackgroundWithBlock: and do the local deletion in the block.

Upvotes: 1

Related Questions