Domanda

Ho tre Uilabel personalizzati in UableViewCell. Il design delle celle come questo,

Date(12.1.2012)  Time(12.00pm)
Cost: $20.00
Details

Ho specificato l'altezza della riga 100 in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;. Ma ora i dettagli a volte restituiscono vuoto (null) dal server. In quel momento ho bisogno di rimuovere l'etichetta dei dettagli dalla cella e cambiare l'altezza della cella (riga) in 70. Come posso farlo? Ho cercato meglio il mio livello in Google. Ma sono ancora solo confuso nel farlo. Potete per favore aiutarmi? Grazie. Ho avuto un'idea da questo link - http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/. Hanno usato solo un'etichetta per ridimensionare l'altezza della riga. Mi aiuti per favore.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
  return 90;
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


            UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
            dateLabel.tag = 100;
            dateLabel.backgroundColor = [UIColor clearColor];
            dateLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
            dateLabel.font = [UIFont boldSystemFontOfSize:16];
            dateLabel.highlightedTextColor = [UIColor whiteColor];
            [cell.contentView addSubview: dateLabel]; 

            UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 200, 25)];
            timeLabel.tag = 101;
            timeLabel.backgroundColor = [UIColor clearColor];
            timeLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            timeLabel.highlightedTextColor = [UIColor whiteColor];
            [cell.contentView addSubview: timeLabel]; 

            UILabel *costLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 25)];
            costLabel.tag = 102;
            costLabel.backgroundColor = [UIColor clearColor];
            costLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            costLabel.font = [UIFont boldSystemFontOfSize:14];
            costLabel.highlightedTextColor = [UIColor whiteColor];
            [cell.contentView addSubview: costLabel];

            UILabel *eventLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 25)];
            eventLabel.tag = 103;
            eventLabel.backgroundColor = [UIColor clearColor];
            eventLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            eventLabel.font = [UIFont boldSystemFontOfSize:14];
            eventLabel.highlightedTextColor = [UIColor whiteColor];
            [cell.contentView addSubview: eventLabel];
         }

        UILabel *dateLabel = (UILabel *) [cell.contentView viewWithTag:100];
        dateLabel.text = [DateArray objectAtIndex:indexPath.row];

        UILabel * timeLabel = (UILabel *) [cell.contentView viewWithTag:101];
        timeLabel.text = [timeArray objectAtIndex:indexPath.row];

        UILabel * costLabel = (UILabel *) [cell.contentView viewWithTag:102];
        costLabel.text = [costArray objectAtIndex:indexPath.row];

        UILabel *eventLabel = (UILabel *) [cell.contentView viewWithTag:103];
        NSString *description = [eventArray objectAtIndex:indexPath.row];
        if([description isEqualToString:@""])
       {
         eventLabel.text = @""; // Here i don't want to show this label and resize(reduce) the row height to 60;
       }
       else
       {
         eventLabel.text = description;
       }
        return cell;
    }

Come posso fare questo? Grazie.

È stato utile?

Soluzione

Durante la restituzione dell'altezza, controlla se la descrizione dei dettagli è vuota. Se è reso 60.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
NSString *description = [eventArray objectAtIndex:indexPath.row];
if([description isEqualToString:@""])
    return 60
else
{
    return 90;
}
}

Inoltre, è necessario rimuovere l'etichetta con @"Details" nella stessa affermazione if in cellForRowAtIndexPath:. Contrassegnalo in anticipo, identificalo dalle sotto -viste e rimuovilo dalla cella.

Altri suggerimenti

È possibile controllare la dimensione della stringa di ritorno utilizzando CGSize come questo -

CGSize size = [detailStr sizeWithFont:[UIFont boldSystemFontOfSize:12.0f] constrainedToSize:CGSizeMake(190, 40) lineBreakMode:UILineBreakModeWordWrap];

Puoi cambiare larghezza, altezza e carattere in base alle tue condizioni. Inoltre, verificare se i dettagli non sono uguali a zero prima di calcolare le dimensioni.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top