简体   繁体   English

TableViewCell-自定义单元格iOS-内容

[英]TableViewCell - Custom Cell iOS - Content

i have a table with a custom cell. 我有一个带有自定义单元格的表。 This cell have a label that i would like move in cell (if a condition is true). 这个单元格有一个标签,我想在单元格中移动(如果条件为真)。 Why the position of label is updated only if the cellForRowAtIndexPath method for that cell is called for the second time? 为什么仅在第二次调用该单元格的cellForRowAtIndexPath方法时才更新标签的位置?

This is the first question. 这是第一个问题。

在此处输入图片说明在此处输入图片说明

The second question is is as follows: The custom cell contain a view, in this view i programmatically add images. 第二个问题如下:自定义单元格包含一个视图,我以编程方式添加图像。 If i have two sections in the table, after scrolling the table, the images of first cell in first section appear in the first cell of second section. 如果我在表中有两个部分,则在滚动表后,第一部分中第一个单元格的图像会出现在第二部分的第一个单元格中。 Why? 为什么?

This is the code 这是代码

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



}

Thanks in advance. 提前致谢。

First Question: Which label? 第一个问题:哪个标签? What condition? 什么情况 Are you talking about this: 您是否在谈论这个:

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

You have to be more specific here! 您必须在这里更具体!

Second Question: The reason that wrong images show up in wrong cells is that your cells are being reused by the tableView . 第二个问题:错误的图像出现在错误的单元格中的原因是tableView正在重用您的单元格。 Make sure to set the custom cell's label.text s and imageView.image s to nil right after dequeueReusableCellWithIdentifier: to ensure they are empty when reused. 确保将自定义单元格的设置label.text S和imageView.image s到nil之后dequeueReusableCellWithIdentifier:确保当再次使用它们是空的。

WARNING: This is NOT a good idea: 警告:不是一个好主意:

[cell.iconeDettaglio addSubview: imageIcona];

Because your cell is being reused, and once reused, you'll see the previous imageIcona and add ANOTHER imageIcona on top as a subview . 由于您的cell已被重用,并且一旦被重用,您将看到前一个imageIcona并在其顶部添加ANOTHER imageIcona作为subview Make the imageIcona an IBOUTLET or property of your custom cell, and only set its image variable in cellForRowAtIndexPath: . 使imageIcona成为自定义单元格的IBOUTLET或属性,并且仅在cellForRowAtIndexPath:设置其image变量。

First question: I can't debug for you, but without running the code, i can imagine that this part of the code 第一个问题:我无法为您调试,但是如果不运行代码,我可以想象这部分代码

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

is never reached the first time because of this other part of code offset = imageIcona.frame.size.width + offset + 2; 因为代码的另一部分,所以从未第一次到达offset = imageIcona.frame.size.width + offset + 2; .

And after you scroll, this loop for (NSString *icona in dettaglioVoto) { is not performed because maybe dettaglioVoto is empty, and so the offset will remain equals to zero, and you will have it the way you want. 滚动后,不会执行for (NSString *icona in dettaglioVoto) {此循环for (NSString *icona in dettaglioVoto) {因为dettaglioVoto可能为空,因此offset将保持等于零,并且可以按照自己的方式进行设置。 But i can't be sure without you debugging your code to see why the if is not being reached the first time. 但是我不确定如果没有调试代码就可以查看为什么第一次没有达到if原因。

Second question: I don't think you are properly using the reuse mechanism for cells. 第二个问题:我认为您没有正确使用单元的重用机制。 So, if you are going to reuse the cells, you should use set prepareForReuse method in the cell code, like this: 因此,如果要重用单元格,则应在单元格代码中使用set prepareForReuse方法,如下所示:

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

Also, it normally works better if you register the nib for the table view, this way: 另外,如果您通过以下方式为表视图注册笔尖,通常效果会更好:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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