Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 重新加载数据-UITableView_Objective C_Ios_Uitableview - Fatal编程技术网

Objective c 重新加载数据-UITableView

Objective c 重新加载数据-UITableView,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我在表格中显示数据时遇到问题,当Tebel更新时调用方法[tv reloadData],但当我选择一行时,它将显示模糊,好像以前的数据没有被删除。我认为问题出在存储SQL查询带来的数据的类中,但我在所有字段中都有一个[release],因此无法工作 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { iMAPArrayProdu

我在表格中显示数据时遇到问题,当Tebel更新时调用方法[tv reloadData],但当我选择一行时,它将显示模糊,好像以前的数据没有被删除。我认为问题出在存储SQL查询带来的数据的类中,但我在所有字段中都有一个[release],因此无法工作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {

    iMAPArrayProdutos *ArrayProdutos = (iMAPArrayProdutos *)[iMAP objectAtIndex:indexPath.row];

    NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];

    iMAPTabela *cell = (iMAPTabela *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    tv.autoresizesSubviews = YES;

    if (cell == nil) {

        cell = [[[iMAPTabela alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 50.0, tableView.rowHeight)] autorelease];

    [cell addColumn:70];

    label.tag = TAG_1; 
    label.font = [UIFont systemFontOfSize:12.0]; 
    label.text = ArrayProdutos.Cod;
    label.textAlignment = UITextAlignmentRight; 
    label.textColor = [UIColor blueColor]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; 

    [cell.contentView addSubview:label]; 

    label =  [[[UILabel alloc] initWithFrame:CGRectMake(80.0, 0, 50.0, tableView.rowHeight)] autorelease]; 

    [cell addColumn:140];

    label.tag = TAG_2; 
    label.font = [UIFont systemFontOfSize:12.0]; 
    label.text = ArrayProdutos.Artrf2;
    label.textAlignment = UITextAlignmentRight; 
    label.textColor = [UIColor blueColor]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; 

    [cell.contentView addSubview:label];

    label =  [[[UILabel alloc] initWithFrame:CGRectMake(145.0, 0, 100.0, tableView.rowHeight)] autorelease]; 

    [cell addColumn:260];

    label.tag = TAG_3; 
    label.font = [UIFont systemFontOfSize:12.0]; 
    label.text = ArrayProdutos.Descri;    
    label.textAlignment = UITextAlignmentRight; 
    label.textColor = [UIColor blueColor]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; 

    [cell.contentView addSubview:label];

    return cell;
}
直接从银行执行查询:

- (void)readiMAPFromDatabase {

    // Setup the database object
    sqlite3 *database;

    // Init the Array
    iMAP = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        // Setup the SQL Statement and compile it for faster access
        sqlite3_stmt *compiledStatement;
        const char *sqlStatement;

        if ([sb.text length] == 0) {

            sqlStatement = "SELECT * FROM APSB1010";

        }
        else {

            NSString *string1;

            if ([OpcaoFiltro isEqualToString:@"1"]) {

                string1 = @"SELECT * FROM APSB1010 WHERE TRIM(COD) LIKE '%";
            }
            else if ([OpcaoFiltro isEqualToString:@"2"]) {

                string1 = @"SELECT * FROM APSB1010 WHERE TRIM(ARTRF2) LIKE '%";
            }
            else if ([OpcaoFiltro isEqualToString:@"3"]) {

                string1 = @"SELECT * FROM APSB1010 WHERE TRIM(DESCRI) LIKE '%";
            }
            else if ([OpcaoFiltro isEqualToString:@"4"]) {

                string1 = @"SELECT * FROM APSB1010 WHERE TRIM(APLICA) LIKE '%";
            }
            else {

                string1 = @"SELECT * FROM APSB1010 WHERE TRIM(APLICA) LIKE '%";
            }

            NSString *string2 = @"%'";
            NSString *result = [[NSString alloc] init];

            result = [result stringByAppendingString:string1];
            result = [result stringByAppendingString:sb.text];
            result = [result stringByAppendingString:string2];

            sqlStatement = [result UTF8String];
        }

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

            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

                // Read the data from the result row
                NSString *ProdCod = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                NSString *ProdPrv1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *ProdGrupo = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                NSString *ProdPicment = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
                NSString *ProdDescri = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
                NSString *ProdArtrf2 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
                NSString *ProdUm = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
                NSString *ProdEmbap = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];
                NSString *ProdDesmax = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];
                NSString *ProdImgap = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)];
                NSString *ProdAplica = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)];
                NSString *ProdQatu_01 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 11)];
                NSString *ProdQatu_11 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 12)];
                NSString *ProdQatu_12 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 13)];
                NSString *ProdQatu_13 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 14)];
                NSString *ProdQatu_14 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 15)];
                NSString *ProdPrprom = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 16)];
                NSString *ProdR_E_C_N_O_ = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 17)];

                // Create a new object with the data from the database
                iMAPArrayProdutos *ArrayProdutos = [[iMAPArrayProdutos alloc] Cod:ProdCod Prv1:ProdPrv1 Grupo:ProdGrupo Picment:ProdPicment Descri:ProdDescri Artrf2:ProdArtrf2 Um:ProdUm Embap:ProdEmbap Desmax:ProdDesmax Imgap:ProdImgap Aplica:ProdAplica Qatu_01:ProdQatu_01 Qatu_11:ProdQatu_11 Qatu_12:ProdQatu_12 Qatu_13:ProdQatu_13 Qatu_14:ProdQatu_14 Prprom:ProdPrprom R_E_C_N_O_:ProdR_E_C_N_O_];

                // Add the object to the Array
                [iMAP addObject: ArrayProdutos];

                [ArrayProdutos release];
            }
        }

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

    sqlite3_close(database);

    [tv reloadData];
}
类,其中存储查询带来的数据:

@implementation iMAPArrayProdutos

@synthesize Cod;
@synthesize Prv1;
@synthesize Grupo;
@synthesize Picment;
@synthesize Descri;
@synthesize Artrf2;
@synthesize Um;
@synthesize Embap;
@synthesize Desmax;
@synthesize Imgap;
@synthesize Aplica;
@synthesize Qatu_01;
@synthesize Qatu_11;
@synthesize Qatu_12;
@synthesize Qatu_13;
@synthesize Qatu_14;
@synthesize Prprom;
@synthesize R_E_C_N_O_;

-(id)Cod:(NSString *)ProdCod Prv1:(NSString *)ProdPrv1 Grupo:(NSString *)ProdGrupo Picment:(NSString *)ProdPicment Descri:(NSString *)ProdDescri Artrf2:(NSString *)ProdArtrf2 Um:(NSString *)ProdUm Embap: (NSString *)ProdEmbap Desmax:(NSString *)ProdDesmax Imgap:(NSString *)ProdImgap Aplica:(NSString *)ProdAplica Qatu_01:(NSString *)ProdQatu_01 Qatu_11:(NSString *)ProdQatu_11 Qatu_12:(NSString *)ProdQatu_12 Qatu_13:(NSString *)ProdQatu_13 Qatu_14:(NSString *)ProdQatu_14 Prprom:(NSString *)ProdPrprom R_E_C_N_O_:(NSString *)ProdR_E_C_N_O_ {

    self.Cod = ProdCod;
    self.Prv1 = ProdPrv1;
    self.Grupo = ProdGrupo;
    self.Picment = ProdPicment;
    self.Descri = ProdDescri;
    self.Artrf2 = ProdArtrf2;
    self.Um = ProdUm;
    self.Embap = ProdEmbap;
    self.Desmax = ProdDesmax;
    self.Imgap = ProdImgap;
    self.Aplica = ProdAplica;
    self.Qatu_01 = ProdQatu_01;
    self.Qatu_11 = ProdQatu_11;
    self.Qatu_12 = ProdQatu_12;
    self.Qatu_13 = ProdQatu_13;
    self.Qatu_14 = ProdQatu_14;
    self.Prprom = ProdPrprom;
    self.R_E_C_N_O_ = ProdR_E_C_N_O_;

    return self;
}

问题可能在这里:

...
[cell.contentView addSubview:label]; 
...
您不断地向单元格添加新标签,即使它被重用(由
dequeueReusableCellWithIdentifier
返回)

应在创建单元格的块中添加标签。稍后,如果重复使用单元格,只需使用指定的标记从单元格中获取标签,并根据新数据更改文本

 if (cell == nil) {
 // this is invoked when dequeueReusableCell returns nil - we actually create the label
    cell = [[[iMAPTabela alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 50.0, tableView.rowHeight)] autorelease];
    [cell addColumn:70];
    label.tag = TAG_1; 
    label.font = [UIFont systemFontOfSize:12.0]; 
    label.text = ArrayProdutos.Cod;
    label.textAlignment = UITextAlignmentRight; 
    label.textColor = [UIColor blueColor]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; 
    [cell.contentView addSubview:label];
}

//this is invoked every time UITableView asks for a cell
UILabel *labelToChange = [cell.contentView viewWithTag:TAG_1];
labelToChange.text = text = ArrayProdutos.Cod;

我很久以前也遇到过同样的问题。这就是它的解决方案。 在添加新标签之前,需要删除现有标签,因为新标签与现有标签重叠

 NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UILabel *subview in subviews) {
        [subview removeFromSuperview];
    }
    [subviews release];
快乐编码。。。
保持饥饿,保持愚蠢。

我的问题是,我在一个视图控制器(一个表视图旋转木马)中有9个表视图,并且对所有这些视图使用一个单元格标识符。我通过为每个tableview单元格分配单独的标识符(在我的例子中,每个tableview都有9个不同的标识符)来解决这个问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ProjectTableviewCell";
    TimesheetTableviewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"ProjectTableviewCell %li",tableView.tag]];
    if (!cell) {
        cell = [[TimesheetTableviewCell alloc] initWithReuseIdentifier:CellIdentifier];
    }

    TimesheetWeek *week = [LoginVC getTimesheetWeeks][tableView.tag];
    Project *project = week.currentData[indexPath.row];
    [cell setCellProject:project];

    cell.delegate = self;

    return cell;
}
之前和之后的屏幕截图:

你好Surjit Joshi,感谢您的合作,知道我们可以依靠帮助总是很好的。