2012-07-29 174 views
0

我有一個導航控制器上有一個表視圖。我想在該導航欄上有兩個按鈕,即「添加到收藏夾」,另一個是「編輯」。表視圖編輯模式

這兩個按鈕都應該觸發使表視圖進入編輯模式的事件。從「添加到收藏夾」按鈕,我希望表格視圖進入插入模式,即在每個單元格前面加上+綠色標誌,並使用編輯按鈕,我希望它進入刪除模式,即每個單元格前都有負號。

我已經整理出來的編輯按鈕,但我無法做到添加到收藏夾按鈕 這裏堅持我的代碼以供參考

viewDidLoad方法:

- (void)viewDidLoad 
{ 

     self.navigationItem.rightBarButtonItem = self.editButtonItem; 

    self.navigationItem.leftBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"Add to Favourite" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)]autorelease]; 




    [super viewDidLoad]; 




- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 
    [self.tableView setEditing:editing animated:YES]; 

    //Do not let the user add if the app is in edit mode. 
    if(editing) 
     self.navigationItem.leftBarButtonItem.enabled = NO; 
    else 
     self.navigationItem.leftBarButtonItem.enabled = YES; 


    NSLog(@"i came till here"); } 

在這種方法我只是從數據庫中檢索值,並從數據庫以及表格視圖中刪除它。

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

    if(editingStyle == UITableViewCellEditingStyleDelete) { 

     NSDictionary *rowVals = (NSDictionary *) [appdelegate.tablearr objectAtIndex:indexPath.row]; 
     NSString *keyValue = (NSString *) [rowVals objectForKey:@"id"]; 

     // [tableView beginUpdates]; 

     sqlite3 *db; 
     int dbrc; //Codice di ritorno del database (database return code) 
     AppDelegate *appDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate; 
     const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String]; 
     dbrc = sqlite3_open(dbFilePathUTF8, &db); 
     if (dbrc) { 
      NSLog(@"Impossibile aprire il Database!"); 
      return; 
     } 

     sqlite3_stmt *dbps; //Istruzione di preparazione del database 

     NSString *deleteStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"Hell\" WHERE id='%@'", keyValue]; 
     const char *deleteStatement = [deleteStatementsNS UTF8String]; 
     dbrc = sqlite3_prepare_v2(db, deleteStatement, -1, &dbps, NULL); 
     dbrc = sqlite3_step(dbps); 
     sqlite3_finalize(dbps); 
     sqlite3_close(db); 


     [appdelegate.tablearr removeObjectAtIndex:indexPath.row]; 



     //Delete the object from the table. 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

這是一個方法,這就是被解僱時,我按下添加到收藏夾按鈕

-(void)saveAction:(UIBarButtonItem *)sender{ 

    NSLog(@"here"); 



} 

現在我應該在這個方法寫,這樣的表視圖進入編輯,並與每個細胞前面的+綠色插入物?

回答

1

爲了把表進入編輯模式,你需要使用:

[self.tableView setEditing:YES animated:YES]; 

要插入和刪除模式之間切換,你需要實現tableView:editingStyleForRowAtIndexPath:。 I.e .:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (some condition) 
     return UITableViewCellEditingStyleInsert; 
    else 
     return UITableViewCellEditingStyleDelete; 
}