1

SQL データベースから取り込まれた UITableView の行を削除しようとしています。

私が使用しているSQLステートメントは、というクラスのものdatabaseHanderClassです。このメソッドは、データベース内の行を削除するための ID である整数 (主キー) を取ります。

-(BOOL) deleteFromDatabase: (NSInteger)delete_id
{
   FMDatabase *dbHandler = [FMDatabase databaseWithPath: [Utility getDatabasePath]];
   BOOL success;
   @try 
   {
        [dbHandler open];        
        success = [dbHandler executeUpdate:@"DELETE FROM inputs WHERE id=%d", delete_id];
        [dbHandler close];
   }
   @catch (NSException *exception) 
   {
       NSLog(@"fejl...%@", exception);
   }
   @finally 
   {
       return success;
   }
}

UITableView での削除に使用している方法は次のとおりです。

- (void)tableView:(UITableView *)tableView commitEditingStyle (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];            

    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) 
    {
         //statement
    }   
}
4

2 に答える 2

0

ではcommitEditingStyle、データの削除と行の削除を一度に行う必要があります。そうしないと、テーブルが矛盾した状態になる可能性があります。

于 2012-08-24T23:19:16.483 に答える
0

次のメソッドを実装する必要があります

データベース コードについては、次を試してください。

-(BOOL) deleteFromDatabase: (NSInteger)delete_id
{
    FMDatabase *dbHandler = [FMDatabase databaseWithPath: [Utility getDatabasePath]];
    BOOL success = NO;

    if(![dbHandler open])
    {
        NSLog(@"Could not open DB");
        return NO; 
    }

    [dbHandler beginTransaction];
    success = [dbHandler executeUpdate:@"DELETE FROM inputs WHERE id = ?", 
    [NSNumber numberWithInt:delete_id]];

    [dbHandler commit]; 
    [dbHandler close];
    return success;
}

「commitEditingStyle」メソッドについては、次のことを試してください。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
      if (editingStyle == UITableViewCellEditingStyleDelete) {
      NSInteger row_number_to_be_deleted = indexPath.row;
      //call method to delete from database
      [DatabaseHandler deleteFromDatabase:row_number_to_be_deleted];

      //now, fetch all details from Database
      self.dataSource = [DatabaseHandler getAllData];
      [tableView reloadData];
}

"NumberOfRowsInSection" の場合:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataSource count];
}

「canEditRowAtIndexPath」の場合:

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

「editingStyleForRowAtIndexPath」の場合:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
       editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Editing Style-DELETE %i", [indexPath row]);
    return UITableViewCellEditingStyleDelete;
}

「commitEditingStyle」の場合:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
 {

     if (editingStyle == UITableViewCellEditingStyleDelete) 
     {
         // Do whatever data deletion you need to do...
         // Delete the row from the data source
         /* This HAS to be changed by YOU */ 
         Employee* emp = [self.dataSource objectAtIndex:[indexPath row]]; 
         NSLog(@"Delete row %i", [indexPath row]);
         [DataEngine removeData:emp];
         [self.dataSource removeObjectAtIndex:[indexPath row]];
     } 
     [self.tableView reloadData];
}
于 2012-08-27T12:35:34.927 に答える