使用行ID检索数据并使用label在表格视图单元格中显示正在覆盖值

时间:2021-10-14 13:08:35

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

我可以通过从数据库中检索数据来在正确的单元格中显示正确的提醒,但问题是第3次提醒被第2次和第4次提醒被覆盖第3次和第3次和第4次提醒被第2次提醒覆盖,依此类推。我使用了不同的标签,并将其作为子视图添加到表格视图的单元格中。这是我的代码,请帮我修复它

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

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

我也正在放置屏幕截图,这将使专家明白这个问题

使用行ID检索数据并使用label在表格视图单元格中显示正在覆盖值

1 个解决方案

#1


1  

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

您正在使用多个sqlite命令从数据库中检索数据。虽然U应该一次性检索所有数据并存储到可变字典和数组中,而不是将其恢复为表格单元格

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

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

{

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

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

        const char *query_stmt = [querySQL UTF8String];

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)

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

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

#1


1  

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

您正在使用多个sqlite命令从数据库中检索数据。虽然U应该一次性检索所有数据并存储到可变字典和数组中,而不是将其恢复为表格单元格

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

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

{

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

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

        const char *query_stmt = [querySQL UTF8String];

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)

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

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