cs193p – Assignment #6 Extra Task #2

Please note, this blog entry is from a previous course. You might want to check out the current one.

Allow users to delete photos (i.e. they no longer appear in tables). You should do it in a way that makes it so that the UIRefreshControl does not bring them back!

Deleting of cells is enabled by providing the appropriate delegate method. There delete the current photo and save the core-data document:

-  (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
 forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        Photo *photo = [self.fetchedResultsController objectAtIndexPath:indexPath];
        [photo.managedObjectContext performBlock:^{
            [photo delete];
            [[SharedDocumentHandler sharedDocumentHandler] saveDocument];
        }];
    }
}


Instead of deleting the photo completely just remove its tags. Add another method to the Photo-entity category. Loop over all tags and if the current photo was the only one, remove the tag. Remove the connection to any other tag and check if the photo was available in the Recent entity.

- (void)delete
{
    for (Tag *tag in self.tags) {
        if ([tag.photos count] == 1) [self.managedObjectContext deleteObject:tag];
    }
    self.tags = nil;
    if (self.recent) [self.managedObjectContext deleteObject:self.recent];
}

The complete code is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.