How do I properly reload data into a table from the local datastore after unpinning an object?

184 views Asked by At

In the iOS app I'm developing I have a UITableView that is populated with objects from the local datastore (I subclassed PFTableViewController for this). I have it set up so that the object is unpinned when the delete button is tapped for that particular row, but after the button is tapped the object is still showing up in the table. The only way to make it disappear is to pull down to refresh or hit the back button and then come back to this view.

I've tried calling [tableview reloadData] and [self loadObjects] to try to get the UITableView to refresh but it doesn't work. I've also tried [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade] but this just causes an error saying there's an "invalid number of rows in section 0."

How can I set it up so that the delete button causes the app to unpin the object and delete the row/refresh the data so that the object won't appear in the table anymore? Thanks. Here's the code:

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
        NSInteger row = [tableView.indexPathForSelectedRow row];
        PFObject *delProfessor = [self.objects objectAtIndex:row];
        [delProfessor unpinInBackground];

        //These are the three ideas I've tried so far
        //[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        //[tableView reloadData];
        //[self loadObjects];
    }
}
1

There are 1 answers

2
Misha On

you should remove object from datasource your self.objects array

[self.objects removeObjectAtIndex: indexPath.row]; [self.tableView reloadData];