2012-01-23 42 views
0

使用核心數據填充我的表格視圖。我沒有得到的是如何從核心數據中刪除單個條目。通過表格刪除核心數據單個條目iPhone

這裏是我使用的代碼:

-(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;} 

在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];} 

在警報視圖的方法:

-(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"); 
}} 

回答

1

如果你想刪除CoreData存儲管理的對象,那麼你應該有:

  1. 參考NSManagedObjectContext從那裏您將刪除對象:context
  2. 參考NSManagedObject要刪除:object

那麼這將是非常簡單的刪除對象:

[context deleteObject:object]; 

你應該知道

  1. 指數的行刪除,例如,i
  2. 從你的數組中檢索它:NSObject *object = [licensePlateArray objectAtIndex:i];
  3. 從數據庫中刪除它:[context deleteObject:object];
  4. 從數組中移除它:[licensePlateArray removeObject:object];
+0

權,但我沒有得到的是如何做到這一點,請幫助我,我花了幾個小時,但仍然在這個問題的解決零個進度 –

+0

告訴一些有關您要刪除 – Nekto

+0

我填充什麼更多的話我的表視圖與核心數據,假設現在表被填充,它顯示A,B,C,D。現在,當用戶在行A或B等長時間和用戶選擇刪除那麼那個行值A或任何數據應該是已刪除 –

0

你必須確定你的NSManagedObject然後致電deleteObject對您的管理dObjectContext。然後這個對象將從您的核心數據中刪除。

但是你必須提供一種機制來「以某種方式」獲取特定tableview行後面的對象。

相關問題