通过表iPhone删除核心数据单个条目

通过表iPhone删除核心数据单个条目,iphone,objective-c,ios,Iphone,Objective C,Ios,使用核心数据填充我的表视图。我不明白的是,如何从核心数据中删除单个条目 以下是我正在使用的代码: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == favouritesTable) { cellValue = [licensePlateArray objectAtIndex:indexPath.r

使用核心数据填充我的表视图。我不明白的是,如何从核心数据中删除单个条目

以下是我正在使用的代码:

-(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;}
在UILongPressGestureRecognitor的方法中:

- (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];}
在警报视图方法中:

-(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");
}}
-(无效)警报视图:(UIAlertView*)警报单击按钮索引:(NSInteger)按钮索引{
NSString*title=[警报按钮索引:按钮索引];
NSManagedObjectContext*contextFav=[app managedObjectContext];
FavoriteSData*Favorites=[NSEntityDescription insertNewObjectForEntityForName:@“FavoriteSData”在托管对象上下文:contextFav];
if([title isEqualToString:@“从收藏夹中删除”])
{
NSLog(@“cellValueForLongPress:%@”,cellValueForLongPress);

如果(cellValueForLongPress您必须识别您的NSManagedObject,然后在ManagedObject上下文中调用deleteObject。那么此对象将从核心数据中删除


但您必须提供一种“以某种方式”将对象放在特定tableview行后面的机制。

如果您想从CoreData存储中删除托管对象,那么您应该:

  • 引用
    NSManagedObjectContext
    ,从中删除对象:
    context
  • 对要删除的
    NSManagedObject
    的引用:
    对象
  • 然后,移除对象将非常简单:

    [context deleteObject:object];
    
    你应该知道

  • 要删除的行的索引,例如,
    i
  • 从数组中检索它:
    NSObject*object=[licensePlateArray objectAtIndex:i];
  • 从数据库中删除它:[上下文删除对象:对象]
  • 从数组中删除它:[licensePlateArray removeObject:object]

  • 是的,但我不明白如何实现这一点,请帮帮我,我已经花了几个小时,但在这个问题上仍然没有任何进展。解决方案再多说几句你想删除什么我正在用核心数据填充我的表视图,假设现在填充了表,并且它显示了A、B、C、D。现在,当用户longpres在A或B行上时,用户选择“删除”,则应删除行值A或任何数据