UITableViewCell - 自定义单元iOS - 内容

时间:2014-01-22 18:34:00

标签: ios uitableview

我有一张带有自定义单元格的表格。这个单元格有一个标签,我想在单元格中移动(如果条件为真)。为什么只有在第二次调用该单元格的cellForRowAtIndexPath方法时才更新标签的位置?

这是第一个问题。

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



}

提前致谢。

2 个答案:

答案 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正在重用您的单元格。确保在label.text之后立即将自定义单元格的imageView.imagenil设置为dequeueReusableCellWithIdentifier:,以确保它们在重复使用时为空。

警告:这是 NOT 一个好主意:

[cell.iconeDettaglio addSubview: imageIcona];

由于您的cell正在重复使用,并且一旦重复使用,您会看到之前的imageIcona并在imageIcona上添加另一个subview。将imageIcona设为自定义单元格的IBOUTLET或属性,并仅在image中设置其cellForRowAtIndexPath:变量。

答案 1 :(得分: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没有第一次到达,我无法确定。

第二个问题:我认为您没有正确使用单元格的重用机制。因此,如果要重用单元格,则应在单元格代码中使用set prepareForReuse方法,如下所示:

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

此外,如果您为表视图注册nib,通常会更好:

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

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