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");



 }

现在我应该在这个方法中写什么,以便表格视图进入编辑状态,并在每个单元格前面插入一个 + 绿色的东西?

4

1 回答 1

1

要将表格置于编辑模式,您需要使用:

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

要在插入和删除模式之间切换,您需要实现tableView:editingStyleForRowAtIndexPath:. IE:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (some condition)
        return UITableViewCellEditingStyleInsert;
    else
        return UITableViewCellEditingStyleDelete;
}
于 2012-07-29T18:26:42.347 回答