【问题标题】:Table View is not reloaded after dismissing edit mode关闭编辑模式后不重新加载表格视图
【发布时间】:2012-04-30 21:11:02
【问题描述】:

我正在尝试使用编辑模式向 TableView 添加新行。使用this tutorial。但是在关闭我的编辑模式视图后,我的表格视图没有重新加载。我的 addItem: 方法

- (IBAction)addItem:(id)sender {

    BIDAppDelegate *appDelegate = (BIDAppDelegate *)[[UIApplication sharedApplication] delegate];

    sqlite3 *database;
    NSString *databaseName;
    NSString *databasePath;

    databaseName = @"TestProjectDatabase.sqlite";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL success = [fileManager fileExistsAtPath:databasePath];

    if(!success) {
        databasePath = [[NSBundle mainBundle] pathForResource:databaseName ofType:nil];
    }

    if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {

        NSString *insertSQL = [NSString stringWithFormat:@"insert into \"MyItems\" ( \"MyItemName\", \"MyItemDescription\") values ( '%@', '%@');", _nameTextField.text, _descriptionTextField.text];
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_stmt *compiledStatement;


        if(sqlite3_prepare_v2(database, insert_stmt, -1, &compiledStatement, NULL) != SQLITE_OK)
        {
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); 
        }else{
            if (sqlite3_step(compiledStatement) == SQLITE_DONE)
            {
                //   NSLog(@"All ok");
            } else {
                //   NSLog(@"FAIL");
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);

    MyItems *itemsToDatabase = [[MyItems alloc] initWithItemName:_nameTextField.text andItemDescription:_descriptionTextField.text];
    [appDelegate.myItems addObject:itemsToDatabase];
    [self dismissModalViewControllerAnimated:YES];
}

SQL 插入没问题,因为重新启动我的项目后新行在哪里。有什么建议可以解决吗?

【问题讨论】:

    标签: objective-c uitableview sqlite


    【解决方案1】:

    假设表数据源从appDelegate.myItems读取,则只需要重新加载表本身[tableView reloadData];

    【讨论】:

    • 我想过。但是我需要在哪种方法中添加[tableView reloadData];
    猜你喜欢
    • 2020-11-22
    • 2020-09-28
    • 1970-01-01
    • 2021-10-23
    • 2021-07-29
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多