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 回答