Omer Waqas Khan
Omer Waqas Khan

Reputation: 2413

Deleting core data single entry through table iPhone

Using core data to populate my table view. The thing I am not getting is that how can I delete a single entry from the core data.

Here is the code I am using:

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

if (tableView == favouritesTable) {
    cellValue = [licensePlateArray objectAtIndex:indexPath.row];
} else { // handle search results table view
    cellValue = [filteredListItems objectAtIndex:indexPath.row];
}

static NSString *CellIdentifier = @"vlCell";

VehicleListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    NSLog(@"Cell Created");

    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"VehicleListCell" owner:nil options:nil];

    for (id currentObject in nibObjects) {
        if ([currentObject isKindOfClass:[VehicleListCell class]]) {
            cell = (VehicleListCell *)currentObject;
        }
    }

    UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
    pressRecongnizer.minimumPressDuration = 0.5f;
    [cell addGestureRecognizer:pressRecongnizer];
    [pressRecongnizer release];
}

cell.textLabel.font = [UIFont systemFontOfSize:10];

Favouritesdata *favdata = [licensePlateArray objectAtIndex:indexPath.row];

[[cell ignition] setImage:[UIImage imageNamed:@"ignition.png"]];
[[cell direction] setImage:[UIImage imageNamed:@"south.png"]];

cell.licPlate.text = [favdata licenseplate];

NSLog(@"cellvalue for cellforRow: %@", cell.licPlate.text);

return cell;}

In the method of UILongPressGestureRecognizer:

- (void)tableCellPressed:(UILongPressGestureRecognizer *)recognizer{

if (recognizer.state != UIGestureRecognizerStateBegan) {
    return;
}

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;

[alert addButtonWithTitle:@"Remove from Favourites"];
[alert addButtonWithTitle:@"Take to Map"];

[alert show];}

In alert view method:

-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {

NSString *title = [alert buttonTitleAtIndex:buttonIndex];

NSManagedObjectContext *contextFav = [app managedObjectContext];
Favouritesdata * favourites = [NSEntityDescription insertNewObjectForEntityForName:@"Favouritesdata" inManagedObjectContext:contextFav];

if([title isEqualToString:@"Remove from Favourites"])
{
    NSLog(@"cellValueForLongPress: %@", cellValueForLongPress);


    if (cellValueForLongPress <= 0) {

        NSLog(@"No data to delete");

    }
    else {

        favourites.licenseplate = cellValueForLongPress;
    }

    [alert dismissWithClickedButtonIndex:0 animated:YES];
}
else if([title isEqualToString:@"Take to Map"])
{
    NSLog(@"Go to MapView");
}

NSError *error;

if (![context save:&error]) {
    NSLog(@"Error Occured");
}}

Upvotes: 0

Views: 826

Answers (2)

Nekto
Nekto

Reputation: 17877

If you want to delete managed object from CoreData storage then you should have:

  1. Reference to NSManagedObjectContext from where you will remove object : context
  2. Reference to NSManagedObject that you want to delete: object

Then it will be very simple to remove object:

[context deleteObject:object];

You should know

  1. index of the row to remove, for example, i.
  2. retrieve it from your array: NSObject *object = [licensePlateArray objectAtIndex:i];
  3. remove it from db : [context deleteObject:object];
  4. remove it from array: [licensePlateArray removeObject:object];

Upvotes: 1

andi1984
andi1984

Reputation: 676

You have to identify your NSManagedObject and then call deleteObject on your managedObjectContext. Then this object will be removed from your core data.

But you have to provide a mechanism to "somehow" get the object behind a specific tableview row.

Upvotes: 0

Related Questions