2011-06-01 7 views
1

Je suis assez loin avec celui-ci, mais je suis suspendu à la partie où j'ai lu les informations sur les chaînes.Problème lors de la détermination de la capacité multiligne UITableViewCell

J'ai fait une cellule qui reçoit des données d'un fichier XML externe, tout fonctionne très bien mais certaines cellules contiennent beaucoup de texte que je veux afficher sur plusieurs lignes. Aussi pas de problème. Mais la partie délicate est la hauteur dynamique de ma cellule. J'ai configuré ceci dans la méthode heightForRowAtIndexPath: mais j'ai besoin de connaître la quantité de texte (rangées) que la cellule contient, et je suis bloqué sur la partie comment la connecter à ma variable cellText (string). Toute aide serait la bienvenue :-)

Voici mon code:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
    } 

    // Set the text in the cell for the section/row. 
    NSString *endDate = [XMLParser stringFromDate:[XMLParser dateFromString:stage.end]]; 
    int endDateLength = endDate.length; 
    NSString *endTime = [NSString stringWithFormat:@"%@", [endDate substringFromIndex:endDateLength -7]]; 

    NSString *startDate = [XMLParser stringFromDate:[XMLParser dateFromString:stage.start]]; 
    int startDateLength = startDate.length; 
    NSString *startTime = [NSString stringWithFormat:@"%@", [startDate substringFromIndex:startDateLength -7]]; 

    NSString *date = [XMLParser stringFromDate:[XMLParser dateFromString:stage.start]]; 
    int dateLength = date.length; 
    NSString *dateString = [NSString stringWithFormat:@"%@", [date substringToIndex:dateLength -7]]; 

    NSString *cellText = nil; 
    NSString *cellExplainText = nil; 

    //Pre title arrays 
    dateTitleArray  = [[NSArray alloc] initWithObjects:@"Dag", @"Start tijd", @"Eind tijd", nil]; 
    nameTitleArray  = [[NSArray alloc] initWithObjects:@"Naam", @"Graad",nil]; 
    addressTitleArray = [[NSArray alloc] initWithObjects:@"Dojo", @"Straat", @"Plaats",nil]; 
    infoTitleArray  = [[NSArray alloc] initWithObjects:@"Kosten", @"Contact", @"Details", nil]; 

    dateArray   = [[NSArray alloc] initWithObjects: dateString, startTime, endTime, nil]; 
    nameArray   = [[NSArray alloc] initWithObjects: stage.teacher, stage.grade, nil]; 
    addressArray  = [[NSArray alloc] initWithObjects: stage.dojo, stage.street, stage.city, nil]; 
    infoArray   = [[NSArray alloc] initWithObjects: stage.cost, stage.contact, stage.details, nil]; 

    switch (indexPath.section) 
    { 
     case 0: 
      cellExplainText = [dateTitleArray objectAtIndex:indexPath.row]; 
      cellText  = [dateArray objectAtIndex:indexPath.row]; 
      break; 
     case 1: 
      cellExplainText = [nameTitleArray objectAtIndex:indexPath.row]; 
      cellText  = [nameArray objectAtIndex:indexPath.row]; 
      break; 
     case 2: 
      cellExplainText = [addressTitleArray objectAtIndex:indexPath.row]; 
      cellText  = [addressArray objectAtIndex:indexPath.row]; 
      break; 
     case 3: 
      cellExplainText = [infoTitleArray objectAtIndex:indexPath.row]; 
      cellText  = [infoArray objectAtIndex:indexPath.row]; 
      break; 
     default: 
      break; 
    } 

    [dateTitleArray release]; 
    [nameTitleArray release]; 
    [addressTitleArray release]; 
    [infoTitleArray release]; 

    [dateArray release]; 
    [nameArray release]; 
    [addressArray release]; 
    [infoArray release]; 

    cell.textLabel.text = cellExplainText; 
    cell.detailTextLabel.text = cellText; 
    cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; 
    cell.detailTextLabel.numberOfLines = 0; 
    cell.detailTextLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0]; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellTextSize = ????????????; 
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0]; 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 
    CGSize labelSize = [cellTextSize sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return labelSize.height + 12; 
} 

Répondre

1
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

CGSize maxSize = CGSizeMake(urMaxSize); 
CGSize cellSize = [itemName sizeWithFont:[UIFont systemFontOfSize:15] 
     constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; 
return cellSize.height; 
} 

itemName est le texte u veulent le remplir avec. Je suppose que c'est [infoTitleArray objectAtIndex:indexPath.row] et l'information de tableau correspondante basée sur l'index. Vous pouvez obtenir la section également dans cette méthode afin que vous puissiez obtenir la chaîne.

Si vous souhaitez utiliser votre méthode

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *cellText; 
switch (indexPath.section) 
    { 
     case 0: 
      cellText = [dateTitleArray objectAtIndex:indexPath.row]; 

      break; 
     case 1: 
      cellText = [nameTitleArray objectAtIndex:indexPath.row]; 

      break; 
     case 2: 
      cellText = [addressTitleArray objectAtIndex:indexPath.row]; 

      break; 
     case 3: 
      cellText = [infoTitleArray objectAtIndex:indexPath.row]; 

      break; 
     default: 
      break; 
    } 

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0]; 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return labelSize.height + 12; 
} 
+0

Salut merci pour l'aide jusqu'à présent. J'ai implémenté le code que vous m'avez fourni. Le bloc de texte ci-dessus. Mais la hauteur dynamique de la cellule ne change pas. Je vois toujours les 3 lignes/ou plus de texte étant pressé dans la cellule de taille standard. Il ne prend pas le nombre de lignes en compte. Aucune suggestion? – iJar

+0

[code] (NSString * celltext; commutateur (indexPath.section) {case 0: celltext = [dateTitleArray objectAtIndex: indexPath.row]; break; cas 1: celltext = [nameTitleArray objectAtIndex: indexPath. ligne]; break; cas 2: celltext = [addressTitleArray objectAtIndex: indexPath.row]; break; cas 3: celltext = [infoTitleArray objectAtIndex: indexPath.row]; break; défaut: break; } ) ... – iJar

+0

[code] (UIFont * cellFont = [UIFont fontWithName: @ "Helvetica" taille: 14.0]; CGSize constraintSize = CGSizeMake (280.0f, MAXFLOAT); CGSize labelSize = [celluleText sizeWithFont: cellFont constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap]; return labelSize.height + 12;) – iJar

1

Je vous suggère de stocker dans un tableau vos valeurs cellText/cellTextExplained, de sorte que dans heightForRowAtIndexPath: vous pouvez les récupérer:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    NSString *cellText = [self.cellTextArray:objectAtIndex:indexPath.row]; 

    //-- rest of your code here 
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0]; 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 
    CGSize labelSize = [cellTextSize sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return labelSize.height + 12; 
} 
+0

Désolé, une question vraiment débutante. J'essaye de placer le cellText/cellTextExplained dans le tableau mais je dois avoir un bloc d'auteurs pour le moment. Comment m'as-tu suggéré de faire ça? – iJar

Questions connexes