2012-01-24 12 views
1

テーブルビューにデータを挿入するためにコアデータを使用する。私が得られないことは、コアデータから単一のエントリを削除する方法です。テーブルビューの行を選択してコアデータエントリを削除する問題

コントローラではなくuitableviewを使用しています。 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.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;} 

を::

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

([タイトルisEqualToString場合は、ここでアラートビュー方式で選択された行は、(削除されます。ここでは

は、私が使用していたコードです@「お気に入りから削除」]))):

-(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); 
///////// to remove the object from core data 
    [licensePlateArray removeObjectAtIndex:alert.tag]; 

} 
else if([title isEqualToString:@"Take to Map"]) 
{ 
    NSLog(@"Go to MapView"); 
} 

NSError *error; 

if (![context save:&error]) { 
    NSLog(@"Error Occured"); 
} 
[favouritesTable reloadData];} 
+0

removalあなたの配列からそのインデックスのオブジェクトを取得し、次にテーブルをリロードします。 –

+0

コード例 –

+0

で助けてください。答えを見てください。私に知らせてください。 –

答えて

1

これを書いてくださいcellForRowAtIndexPath

UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)]; 
    pressRecongnizer.view.tag = indexPath.row; 
    pressRecongnizer.minimumPressDuration = 0.5f; 
    [cell addGestureRecognizer:pressRecongnizer]; 
    [pressRecongnizer release]; 

-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex

[YourArray removeObjectAtIndex:alert.tag];  
[YourTable reloadData]; 

でこれを書き、この

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

ようalertview使用

...あなたのコード内の適切な場所で幸せなコーディングをこのコードを使用してください。 。

+0

ありがとうございました。私の日を救ってくれました...ありがとうございました。 –

+0

今私は持っていますもう1つの問題は、エントリが削除され、新しいテーブルが正しくロードされますが、今私は全体のビューを再ロードすると、削除されたエントリを再び表示しています...何が問題になる可能性があります.. –

+0

ur側..コードをデバッグし、結果を参照してください。 –

関連する問題