开发者

implement tableview to show the data of database

I am making a small iPhone application in which I have implemented the database concept which is used to store name, contact no, email id. I Want that whenever I user save any contact It get displayed on table view. In -(IBAction)retriveall:(id)sender action I am retriving all the data from database and storing it into array. Now I want to display all the data in tableview.

How can I do this? Please help me.

-(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
{

 开发者_如何学Python   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 {
    }


}


You should declare an mutable array as an instance variable something like,

NSMutableArray * colors;

Initialize it in the viewDidLoad method and later alter your retrive1 method to add to the colors array.

-(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 { }
}

And you'll have to implement the rest of the table view methods in the usual manner using the array as the source.


You have to create object with all data and implementation of UITableViewDataSource protocol for table view and set it in TableView property dataSource. After you havecall UITableView method -(void)reloadData;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜