2011-06-21 15 views
1

名前、連絡先、電子メールIDを格納するために使用されるデータベースコンセプトを実装した小さなiPhoneアプリケーションを作成しています。私はいつでも私はユーザーが任意の連絡先を保存すると、それはテーブルビューに表示されます。 - (IBAction)retriveall:(id)送信者アクション私はデータベースからすべてのデータを取得し、配列に格納しています。今、私はすべてのデータをtableviewに表示したいと思います。データベースのデータを表示するテーブルビューを実装します

どうすればいいですか?私を助けてください。

-(IBAction)retriveall:(id)sender 
{ 
    [self retrive2]; 
    for (int i =0; i< [namearray count]; i++) 
    { 
     NSLog(@"Name is:%@",[namearray objectAtIndex:i]); 
     NSLog(@"Password is:%@",[passarray objectAtIndex:i]); 
    } 
} 



-(IBAction)retrive:(id)sender 
{ 
    [self retrive1]; 
    two.text = databasecolorvalue; 
    UIAlertView *favAlert=[[UIAlertView alloc] initWithTitle:@"" message:@"Retrived" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
    [favAlert show]; 
    [favAlert release]; 
} 

-(void)retrive1 
{ 

    databaseName = @"med.sqlite"; 

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

    @try { 

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

      const char *sqlStatement="select pswd from Login where username = ? "; 
      sqlite3_stmt *compiledStatement; 

      if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 

       // Loop through the results and add them to the feeds array 
       // Create a new animal object with the data from the database 
       sqlite3_bind_text(compiledStatement, 1, [one.text UTF8String], -1, SQLITE_TRANSIENT); 

       while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 

        databasecolorvalue =[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,0)]; 

       } 

      } 
      // Release the compiled statement from memory 
      sqlite3_finalize(compiledStatement); 

     } 
     sqlite3_close(database); 
    } 
    @catch (NSException * ex) { 
     @throw ex; 
    } 
    @finally { 
    } 


} 

答えて

0

あなたは

NSMutableArray * colors; 

は、色の配列に追加するには、あなたの retrive1方法を変更し、後で viewDidLoad方法でそれを初期化し、インスタンス変数何かなどの可変配列を宣言する必要があります。

-(void)retrive1 
{ 
    /* Clearing existing values for newer ones */ 
    [colors removeAllObjects] 

    /* Get database path */ 

    @try { 
      [..] 
       while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
        [colors addObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,0)]]; 
       } 

      [..] 
    } 
    @catch (NSException * ex) { @throw ex; } 
    @finally { } 
} 

そして、配列をソースとして使用して、通常の方法で残りのテーブルビューメソッドを実装する必要があります。

0

すべてのデータを持つオブジェクトを作成し、テーブルビュー用のUITableViewDataSourceプロトコルの実装を作成し、TableViewプロパティのdataSourceに設定する必要があります。 UITableViewメソッドがあると、 -(void)reloadData;

関連する問題