2014-01-22 98 views
0

我有一個自定義單元格的表格。這個單元格有一個標籤,我希望在單元格中移動(如果條件成立)。爲什麼僅當第二次調用該單元格的cellForRowAtIndexPath方法時才更新標籤的位置?TableViewCell - 自定義單元格iOS - 內容

這是第一個問題。

enter image description here enter image description here

第二個問題是如下: 定製細胞包含的視圖,在該視圖中以編程增加圖象。如果我在表格中有兩個部分,在滾動表格後,第一部分中的第一個單元格的圖像出現在第二部分的第一個單元格中。爲什麼?

這是代碼提前

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

NSString *idDettCal = @"cellaVotoCalciatore"; 
NSString *idRiepilogo = @"riepilogoCell"; 
NSString *idTotali = @"bonusTotaliCell"; 

//Dictionary per Titolari e Panchinari 
NSDictionary *calciatore = [[NSDictionary alloc] init]; 




if (indexPath.section == 0 || indexPath.section == 1) { 

    foaVotoCalciatoreCell *cell = (foaVotoCalciatoreCell *)[tableView dequeueReusableCellWithIdentifier:idDettCal]; 

    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:idDettCal owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 


    //TITOLARI 
    if (indexPath.section == 0) { 
     calciatore = [titolari objectAtIndex:indexPath.row]; 

    } 
    //PANCHINARI 
    else if (indexPath.section == 1) { 
     calciatore = [panchinari objectAtIndex:indexPath.row]; 
    } 

    NSString *calciatoreStr = 
    [NSString stringWithFormat:@"%@%@%@%@", 
    [calciatore objectForKey:@"CALCIATORE"], 
    @" (", 
    [[calciatore objectForKey:@"SQUADRA"] substringToIndex:3], 
    @")"]; 


    NSString *imgRuolo = 
    [NSString stringWithFormat:@"%@%@", 
    [calciatore objectForKey:@"RUOLO"], 
    @".jpg"]; 

    cell.ruolo.image = [UIImage imageNamed:imgRuolo]; 


    NSDictionary *dettaglioVoto = [[NSDictionary alloc] init]; 
    UIImageView *imageIcona; 

    CGFloat offset; 
    dettaglioVoto = [calciatore objectForKey:@"VOTO_STM"]; 



    offset = 0; 
    for (NSString *icona in dettaglioVoto) { 
     if ([icona isEqualToString: @"AMMONITO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"amm.png"]]; 
     } 
     else if ([icona isEqualToString: @"GOL_FATTO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"golfatto.png"]]; 
     } 
     else if ([icona isEqualToString: @"GOL_SUBITO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"golsubito.png"]]; 
     } 
     else if ([icona isEqualToString: @"ENTRATO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"entrato.png"]]; 
     } 
     else if ([icona isEqualToString: @"USCITO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"uscito.png"]]; 
     } 
     else if ([icona isEqualToString: @"ESPULSO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"esp.png"]]; 
     } 
     else if ([icona isEqualToString: @"ASSIST"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"assist.png"]]; 
     } 
     else if ([icona isEqualToString: @"ASSIST_FERMO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"assistf.png"]]; 
     } 
     else if ([icona isEqualToString: @"RIGORE_SEGNATO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rigoresegnato.png"]]; 
     } 
     else if ([icona isEqualToString: @"RIGORE_SBAGLIATO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rigoresbagliato.png"]]; 
     } 
     else if ([icona isEqualToString: @"RIGORE_PARATO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rigoreparato.png"]]; 
     } 
     else if ([icona isEqualToString: @"AUTOGOL"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"autogol.png"]]; 
     } 
     else if ([icona isEqualToString: @"GOL_PARTITA"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"golvittoria.png"]]; 
     } 
     else if ([icona isEqualToString: @"GOL_PAREGGIO"]) { 
      imageIcona = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"golpareggio.png"]]; 
     } 

     imageIcona.frame = CGRectMake(offset, 0, imageIcona.frame.size.width, imageIcona.frame.size.height); 
     offset = imageIcona.frame.size.width + offset + 2; 
     [cell.iconeDettaglio addSubview: imageIcona]; 
    } 

    if (offset == 0) { 
     cell.calciatore.frame = CGRectMake(cell.calciatore.frame.origin.x, 14, cell.calciatore.frame.size.width, cell.calciatore.frame.size.height); 
     cell.iconeDettaglio.hidden = YES; 
    } 

    cell.calciatore.text = calciatoreStr; 
    cell.votoIniziale.text = [calciatore objectForKey:@"VOTO_PRNT"]; 
    cell.votoFinale.text = [calciatore objectForKey:@"VOTO_PRNT_TOT"]; 
    return cell; 

} 
//La giornata è calcolata. Sezione 2 = BONUS e TOTALI 
else if (variabiliGlobali.gioCalcolata && indexPath.section == 2) { 

    NSDictionary *totaliVal; 
    NSDictionary *totaliTip; 

    totaliVal = [logCalValore objectAtIndex:indexPath.row]; 
    totaliTip = [logCalTipo objectAtIndex:indexPath.row]; 

    foaBonusTotaliCell *cell = (foaBonusTotaliCell *)[tableView dequeueReusableCellWithIdentifier:idTotali]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:idTotali owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.tipoBonusLabel.text = totaliTip; 
    cell.totaleLabel.text = totaliVal; 

    return cell; 

} 

else { 
    NSDictionary *riepilogoVal; 
    NSDictionary *riepilogoTip; 

    riepilogoVal = [logForValore objectAtIndex:indexPath.row]; 
    riepilogoTip = [logForTipo objectAtIndex:indexPath.row]; 

    foaRiepilogoCell *cell = (foaRiepilogoCell *)[tableView dequeueReusableCellWithIdentifier:idRiepilogo]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:idRiepilogo owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.voceRiepilogoLabel.text = riepilogoTip; 
    cell.valoreRiepilogoLabel.text = riepilogoVal; 
    return cell; 
} 



} 

感謝。

+1

您需要發佈相關代碼,以便我們可以幫助您解決問題。 – rmaddy

+0

我已發佈代碼 –

+0

我已插入圖片。第一個是表格出現的時候。第二個是在滾動之後。 –

回答

0

第一個問題:哪個標籤?什麼條件?你在說這個:

if (offset == 0) { 
     cell.calciatore.frame = CGRectMake(cell.calciatore.frame.origin.x, 14, cell.calciatore.frame.size.width, cell.calciatore.frame.size.height); 
     cell.iconeDettaglio.hidden = YES; 
} 

你必須在這裏更具體!

第二個問題:是錯誤的圖像錯誤的細胞出現的原因是,你的細胞被tableView重用。請務必在dequeueReusableCellWithIdentifier:之後立即將自定義單元的label.text s和imageView.image s設置爲nil,以確保它們在重新使用時爲空。

警告:這是不是一個好主意:

[cell.iconeDettaglio addSubview: imageIcona]; 

因爲你cell被重複使用,重複使用一次,你會看到以前imageIcona並在上面添加另一個imageIcona作爲subview。使imageIconaIBOUTLET或您的自定義單元格的屬性,並且只在cellForRowAtIndexPath:中設置其image變量。

+0

第一個問題:如果我沒有標籤下的圖像偏移量是0,所以我想在單元格中間移動標籤。在標籤「布馮(JUV)」的第一張圖片中,沒有圖像。爲什麼只有在滾動表格後標籤才移動到單元格的中間位置?對於第二個問題,我不知道將添加到查看「cell.iconeDettaglio」的圖像的數量,所以我怎麼能做一個IBOUTLET?我的IBOUTLET在iconeDettaglio上。 –

+0

@MarcoGrieco:我仍然不確定你的代碼在哪裏,但可以嘗試調用'[self.tableView setNeedsDisplay]'或[cell setNeedsDisplay]'。 如果這些工作都不起作用,您還可以在'tableView'上調用'reloadRowsAtIndexPaths:'來重新加載這些單元。 – Armin

0

第一個問題:我不能調試你,但不運行的代碼,我可以想像的是,這部分代碼

if (offset == 0) { 
    cell.calciatore.frame = CGRectMake(cell.calciatore.frame.origin.x, 14, cell.calciatore.frame.size.width, cell.calciatore.frame.size.height); 
    cell.iconeDettaglio.hidden = YES; 
} 

永遠達不到,因爲這另一部分的第一次的代碼 offset = imageIcona.frame.size.width + offset + 2;

而你滾動後,此循環for (NSString *icona in dettaglioVoto) {不執行,因爲可能dettaglioVoto是空的,所以offset將保持等於零,你會按你想要的方式。但我不能確定沒有你調試你的代碼,看看爲什麼if第一次沒有被達到。

第二個問題:我不認爲你正在正確使用單元的重用機制。所以,如果你要重用細胞,你應當使用prepareForReuse方法在單元代碼,就像這樣:

-(void) prepareForReuse { 
    // Your code code here: like clearing old images and text and/or setting new images or text 
} 

而且,它通常工作更好,如果你註冊筆尖爲表視圖,這樣:

foaVotoCalciatoreCell *cell = (foaVotoCalciatoreCell *)[tableView dequeueReusableCellWithIdentifier:idDettCal]; 

if (cell == nil) 
{ 
    [[self tableView] registerNib:[UINib nibWithNibName:loadNibNamed:idDettCal bundle:nil] 
     forCellReuseIdentifier:nibWithNibName:loadNibNamed:idDettCal]; 
} 
+0

第一個問題:如果我沒有標籤下的圖像偏移量是0,所以我想在單元格中間移動標籤。在標籤「布馮(JUV)」的第一張圖片中,沒有圖像。爲什麼只有在滾動表格後標籤才移動到單元格的中間位置? –

相關問題