2012-04-19 58 views
0

我试图用编辑模式向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插入是OK,因为重新启动我的项目后,新行在哪里。任何建议如何解决它?

回答

1

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

+0

我想过了。但在哪种方法中,我需要添加'[tableView reloadData];'? – RomanHouse 2012-04-19 11:37:23

+0

更新数据源后,您可以调用它。 – ssteinberg 2012-04-19 15:02:42