2012-04-19 22 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の挿入は問題ありません。どのようにそれを修正するための任意の提案?

答えて

1

テーブルデータソースがappDelegate.myItemsから読み込むと仮定すると、テーブル自体をリロードする必要があります。[tableView reloadData];

+0

私はそれについて考えました。しかし、どのメソッドで '[tableView reloadData];'?を追加する必要がありますか? – RomanHouse

+0

データソースを更新した後、データソースを呼び出します。 – ssteinberg

関連する問題