2012-07-29 125 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; 
}