Iphone 通过在表视图上选择一行来删除核心数据条目的问题

Iphone 通过在表视图上选择一行来删除核心数据条目的问题,iphone,objective-c,ios,cocoa-touch,Iphone,Objective C,Ios,Cocoa Touch,使用核心数据填充我的表视图。我不明白的是,如何从核心数据中删除单个条目 我使用的是uitableview,而不是控制器 以下是我正在使用的代码: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == favouritesTable) { cellValue = [licensePlateArray o

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

我使用的是uitableview,而不是控制器

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

-(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.view.tag = indexPath.row;
    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;
}

VehicleListCell* cell = (VehicleListCell *)[recognizer view];

cellValueForLongPress = cell.licPlate.text;

NSLog(@"cell value: %@", cellValueForLongPress);

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;
alert.tag = recognizer.view.tag;
[alert addButtonWithTitle:@"Remove from Favourites"];
[alert addButtonWithTitle:@"Take to Map"];

[alert show];}
在此警报视图方法中,所选行将被删除(如果([title isEqualToString:@“从收藏夹中删除”]):


cellforrowatinexpath

    UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
    pressRecongnizer.view.tag = indexPath.row;
    pressRecongnizer.minimumPressDuration = 0.5f;
    [cell addGestureRecognizer:pressRecongnizer];
    [pressRecongnizer release];
然后像这样使用alertview

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;
alert.tag = recognizer.view.tag;
[alert addButtonWithTitle:@"Remove from Favourites"];
[alert addButtonWithTitle:@"Take to Map"];    
[alert show];
然后将其写入
-(void)alertView:(UIAlertView*)警报单击按钮索引:(NSInteger)按钮索引中

[YourArray removeObjectAtIndex:alert.tag];    
[YourTable reloadData];
请在代码中的适当位置使用此代码


快乐编码..

cellforrowatinexpath

    UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
    pressRecongnizer.view.tag = indexPath.row;
    pressRecongnizer.minimumPressDuration = 0.5f;
    [cell addGestureRecognizer:pressRecongnizer];
    [pressRecongnizer release];
然后像这样使用alertview

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil] ;
alert.tag = recognizer.view.tag;
[alert addButtonWithTitle:@"Remove from Favourites"];
[alert addButtonWithTitle:@"Take to Map"];    
[alert show];
然后将其写入
-(void)alertView:(UIAlertView*)警报单击按钮索引:(NSInteger)按钮索引中

[YourArray removeObjectAtIndex:alert.tag];    
[YourTable reloadData];
请在代码中的适当位置使用此代码


快乐编码..

从数组中的索引中删除该对象,然后重新加载表。您能帮我编写代码示例吗请查看答案,如果未获取,请告诉我从数组中删除该对象,然后重新加载表。您能帮我编写代码示例吗请查看答案,如果未获取,请让我知道我知道现在我又遇到了一个问题,条目被删除,新表被正确加载,但现在当我重新加载整个视图时,它会再次显示已删除的条目。。。有什么问题你能帮我吗。。。。问题所在,如应用程序内委托方法或其他方法,因为我将数据保存在一个控制器中,并在另一个控制器中获取数据,因为在cellForRowAtIndexPath中,表是从核心数据填充的,如果删除了该对象,则核心数据应为空。然后,从UITableView中使用的根数组中删除该对象。如何删除从表视图中删除的每个对象,也应从核心数据中删除使用:removeObjectAtIndex for arrayNow我还有一个问题,条目被删除,新表被正确加载,但现在当我重新加载整个视图时,它会再次显示已删除的条目。。。有什么问题你能帮我吗。。。。问题所在,如应用程序内委托方法或其他方法,因为我将数据保存在一个控制器中,并在另一个控制器中获取数据,因为在cellForRowAtIndexPath中,表是从核心数据填充的,如果删除了该对象,则核心数据应为空。然后,从UITableView中使用的根数组中删除该对象。如何删除从表视图中删除的每个对象,还应使用:removeObjectAtIndex for array从核心数据中删除