2011-09-22 172 views
2

我在顯示錶格中的數據時遇到問題,在Tebel更新時調用方法[tv reloadData],但是當我選擇一行時,它將顯得模糊,就像以前的數據沒有被刪除一樣。我認爲問題在於存儲由我的SQL查詢帶來的數據的類,但是我在所有字段中都有一個[發佈],並且不起作用。在那裏我存儲的查詢帶來的數據reloadData - UITableView

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

類:

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

直接從銀行進行查詢

@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; 
} 

回答

5

這個問題可能是在這裏:

... 
[cell.contentView addSubview:label]; 
... 

你連續添加ne w標籤即使重複使用(由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; 
+0

作品對我來說相當不錯 –

0

很久以前我得到同樣的問題。這是它的解決方案。 由於新標籤在現有標籤上重疊,因此您需要在添加新標籤之前刪除現有標籤。

NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews]; 
    for (UILabel *subview in subviews) { 
     [subview removeFromSuperview]; 
    } 
    [subviews release]; 

快樂編碼... 求知若飢,虛心若愚。

+0

好日子Surjit喬希, 謝謝您的合作,這是一件好事,知道我們可以幫助計算。 – user941059

+0

hello user941059 ..你有沒有使用上面的代碼? –

+0

我結束了使用代碼mja。感謝您的答覆。 – user941059

0

我的問題是我在一個視圖控制器(tableviews的旋轉木馬)中有9個tableviews,並且它們都使用了一個單元格標識符。我通過爲每個tableview單元分配單獨的標識符(在我的例子中爲每個tableview指定不同的標識符)來解決這個問題。後

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

截圖之前&: enter image description here enter image description here