Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

iphone - Populate the table view sections with rows of table in sqlite database in an order

I have 2 view controller pages.Add Reminder page which contains save button as right bar button of navigation bar

I have earlier got issues regarding number of sections in table view for view reminder page based on number of saves in add reminder page:

I found out the solution for generating cells based on number of saved reminders

this is the code:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)view
{
NSInteger numTableRecords = -1;
    sqlite3_stmt *statement;
    if (sqlite3_open([self.databasePath UTF8String], &remindersDB) == SQLITE_OK) 
    {
        NSString *sqlStatement = [NSString stringWithFormat: @"select count(*) from reminders"];
        const char *sql = [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding];
        if(sqlite3_prepare_v2(remindersDB, sql, -1, &statement, NULL) == SQLITE_OK) 
        {          
            while(sqlite3_step(statement) == SQLITE_ROW) 
            {
                numTableRecords = sqlite3_column_int(statement, 0);
            }
        }
        else 
        {
            printf("could not prepare statement: %s
", sqlite3_errmsg(remindersDB));
        }
    }

    else 
    {
        NSLog(@"Error in Opening Database File");
    }
    sqlite3_close(remindersDB);
    return numTableRecords; 
}

Now my problem here is I am able to retrieve the data,but not in a proper way.I mean this is the code I used to retrieve the data

if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellId] autorelease];
        view.backgroundColor = [UIColor clearColor];
        cell.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"reminderbutton.png"]];

        label1 = [[[UILabel alloc]initWithFrame:CGRectMake(26, 3, 30, 40)]autorelease];
        label1.backgroundColor = [UIColor clearColor];
        label1.textColor = [UIColor whiteColor];

        label2 = [[[UILabel alloc]initWithFrame:CGRectMake(45, 3, 100, 40)]autorelease];
        label2.backgroundColor = [UIColor clearColor];
        label2.textColor = [UIColor whiteColor];

        label3 = [[[UILabel alloc]initWithFrame:CGRectMake(119, 3, 100, 40)]autorelease];
        label3.backgroundColor = [UIColor clearColor];
        label3.textColor = [UIColor whiteColor];

        label4 = [[[UILabel alloc]initWithFrame:CGRectMake(198, 3, 120, 40)]autorelease];
        label4.backgroundColor = [UIColor clearColor];
        label4.textColor = [UIColor whiteColor];

        //Retrieve the values of database
        const char *dbpath = [self.databasePath UTF8String];
        sqlite3_stmt *statement;

        if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK)
        {
            NSString *querySQL = [NSString stringWithFormat:@"SELECT * from reminders ORDER BY id"];

            const char *query_stmt = [querySQL UTF8String];

            if (sqlite3_prepare_v2(self.remindersDB ,query_stmt , -1, &statement, NULL) == SQLITE_OK)
            {
                if (sqlite3_step(statement) == SQLITE_ROW)
                {
                    NSString *ID = [[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]autorelease];

                    NSString *nameField = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]autorelease];                    

                    NSString *eventField = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]autorelease];

                    NSString *dateField = [[[NSString alloc]initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]autorelease];

                    label1.text = ID;
                    label2.text = nameField;
                    label3.text = eventField;
                    label4.text = dateField;
                } 

                sqlite3_finalize(statement);
            }
            sqlite3_close(self.remindersDB);
        }


                    [cell addSubview:label1];
                    [cell addSubview:label2];
                    [cell addSubview:label3];
                    [cell addSubview:label4];

    }

    return cell;

I have inserted 4 reminders,the 4 cells are appearing,but the problem is with displaying

It is appearing as following

enter image description here

How can I get the 4 reminders displayed in 4 cells consecutively,I mean the 1st reminder on 1st,2nd reminder on 2nd and so on..

Please help me to fix this issue,thanks all in advance :)

Sorry all...I found out the complete solution to the issue in the following link:

Retrieve all rows inserted in sqlite database and display in table view cells containing labels as subviews with different sections

Sorry for the question...Thanks every one who viewed this,take it If useful

Hence I am not removing this question,bye TC :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm giving you one suggestion just try it. I hope it will work.

Do one thing, just retrieve data from sqlite in viewDidAppear or viewWillAppear and save the data in an nsmutableArray.

Then in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section just return [yourArray count].

and for reloading table just try with [self.tableview reloadData]

I hope it'll be work. just let me know for any further clarification.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...