2012-01-23 10 views
0

コアデータを使用してテーブルビューにデータを入力する。私が得られないことは、コアデータから単一のエントリを削除する方法です。あなたのNSManagedObjectを特定する必要がテーブルからコアデータを1つ削除する

-(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ストレージから管理対象オブジェクトを削除したいなら、あなたは持っている必要があります:あなたがオブジェクトを削除する場所からNSManagedObjectContext

  1. 参考:削除したいNSManagedObjectcontext
  2. 参考:object

オブジェクトを削除するのが非常に簡単です:

[context deleteObject:object]; 

あなたは、例えば、iを削除する行の

  1. インデックスを知っている必要があります。
  2. あなたの配列からそれを取り出してください:NSObject *object = [licensePlateArray objectAtIndex:i];
  3. これをdb:[context deleteObject:object]から削除してください。
  4. 配列から削除します。[licensePlateArray removeObject:object];
+0

右が、私はこれを実現する方法ということになっていないです、私を助けてください、私は時間を過ごしたが、それでも私は移入していますあなたが –

+0

の進展をゼロにしています私のテーブルビューでは、コアデータを使用して、テーブルにデータが入力され、A、B、C、Dが表示されていると仮定します。 – Nekto

+0

を削除したい何をすべきかについてのいくつかのより多くの単語を知らせるこの問題の解決 –

0

:UILongPressGestureRecognizerの方法で

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

:アラートの表示方法では

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

ここ

は、私が使用していたコードです あなたの管理下のdeleteObjectに電話してくださいdObjectContext。このオブジェクトは、コアデータから削除されます。

しかし、何らかの理由で特定のテーブルビュー行の背後にオブジェクトを取得するメカニズムを提供する必要があります。

関連する問題