表格视图编辑模式

时间:2012-07-29 17:45:40

标签: objective-c xcode4

我有一个导航控制器,其上有一个表格视图。我想要导航栏上的两个按钮,即"添加到收藏夹"另一个是"编辑"。

该按钮都应该触发使表视图进入编辑模式的事件。从"添加到收藏夹按钮,我希望表格视图进入插入模式,即每个单元格前面的+绿色标志,并使用编辑按钮,我希望它进入删除模式,即 - 前面的负号每个细胞。

我已整理出“编辑”按钮,但我无法执行“添加到收藏夹”按钮。 在此处粘贴我的代码以供参考

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 个答案:

答案 0 :(得分:1)

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

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

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

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