Sqlite数据库并滑动删除

时间:2011-01-09 13:26:21

标签: ios iphone sqlite tableview sql-delete

我正在处理的项目要求我使用sqlite数据库,而且我一直试图通过滑动删除来处理我的tableView:

[self performSelectorOnMainThread:@selector(removeMovieFromCache:) withObject:[NSNumber numberWithInt:movieId] waitUntilDone:YES];
    [db performSelectorOnMainThread:@selector(performQuery:) withObject:[NSString stringWithFormat:@"DELETE FROM movie WHERE id = %d", movieId] waitUntilDone:YES];
    [db performSelectorOnMainThread:@selector(performQuery:) withObject:[NSString stringWithFormat:@"DELETE FROM new_movies WHERE new_movie_id = %d", movieId] waitUntilDone:YES];
    [self removeMovieFromCache:movieId];
    [db performQueryWithFormat:@"DELETE FROM movie WHERE id = %d", movieId];
    [db performQueryWithFormat:@"DELETE FROM new_movies WHERE new_movie_id = %d", movieId];
    [db performQuery:@"COMMIT"];

这是从我的数据库中删除某些东西的代码。当我尝试将此应用于我的滑动删除命令:

-

(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)movieID
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {        

      //code goes here        
    }
}

它只是不想工作,我做错了什么?

1 个答案:

答案 0 :(得分:2)

在tableView数据源中,尝试实现:

(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath 
{
   return UITableViewCellEditingStyleDelete;
}


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

(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)movieID
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {       
       // First delete the row from the table, then delete from the DB, finally reload the date in the table 
       [theTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
       // code to delete from DB
       [theTable reloadData];        
    }
}

(将“theTable”替换为你所谓的桌子!)