简体   繁体   中英

Retrieving the data using row ids and displaying in table view cells using label is making values overwritten

I am able to display the correct reminders in correct cells by retrieving data from database,but the problem is 3rd reminder is getting overwritten with 2nd and 4th reminder is getting overwritten with 3rd and 3rd and 4th reminder is getting overwritten with 2nd reminder and so on.I have used different labels and added as subview to cells of table view.Here is my code,please help me to fix it

NSString *CellId = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];

UITableViewCell *cell = (UITableViewCell *)[view dequeueReusableCellWithIdentifier:CellId];

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

    if (indexPath.section == 0)
    {            
        //Retrieve the values of database
        const char *dbpath = [self.databasePath UTF8String];
        sqlite3_stmt *statement;


        switch (indexPath.row) 
        {
            case 0:
            {

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

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

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

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

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

                    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];
            }
                break;

            case 1:
            {

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

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

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

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


                if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK)
                {
                    NSString *querySQL = [NSString stringWithFormat:@"SELECT * from reminders order by id limit 1 offset 1"];

                    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];

                            label5.text = ID;
                            label6.text = nameField;
                            label7.text = eventField;
                            label8.text = dateField;
                        } 

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

                [cell addSubview:label5];
                [cell addSubview:label6];
                [cell addSubview:label7];
                [cell addSubview:label8];

            }

            case 2:
            {

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

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

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

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

                if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK)
                {
                    NSString *querySQL = [NSString stringWithFormat:@"SELECT * from reminders order by id limit 1 offset 2"];

                    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];

                            label9.text = ID;
                            label10.text = nameField;
                            label11.text = eventField;
                            label12.text = dateField;
                        } 

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

                [cell addSubview:label9];
                [cell addSubview:label10];
                [cell addSubview:label11];
                [cell addSubview:label12];

            }

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

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

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

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

                if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK)
                {
                    NSString *querySQL = [NSString stringWithFormat:@"SELECT * from reminders order by id limit 1 offset 3"];

                    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];

                            label13.text = ID;
                            label14.text = nameField;
                            label15.text = eventField;
                            label16.text = dateField;
                        } 

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

                [cell addSubview:label13];
                [cell addSubview:label14];
                [cell addSubview:label15];
                [cell addSubview:label16];

            }

I am also placing screen shot that will make experts understand about the issue clear

在此处输入图片说明

You are using multiple sqlite commands for retrieving data from database. While U should to retrieve all the data in a single time and store into a mutable Dictionary and array, than get it back for table cell

if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK)

{

NSString *querySQL = [NSString stringWithFormat:@"SELECT * from reminders"];

const char *query_stmt = [querySQL UTF8String];

if (sqlite3_prepare_v2(self.remindersDB ,query_stmt , -1, &statement, NULL) == SQLITE_OK) { while (sqlite3_step(compiledStatement) == 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];

                    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:ID,@"ID",nameField,@"nameField",………….];

                    [arrObj addObject:dic]; // arrObj is a mutable arr

                }
            }

        sqlite3_finalize(compiledStatement);
    }

} sqlite3_close(database);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM