Reputation: 2413
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
Reputation: 17877
If you want to delete managed object from CoreData storage then you should have:
NSManagedObjectContext
from where you will remove object : context
NSManagedObject
that you want to delete: object
Then it will be very simple to remove object:
[context deleteObject:object];
You should know
i
. NSObject *object = [licensePlateArray objectAtIndex:i];
Upvotes: 1
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