Redimensionner UITableViewCell à la hauteur de UILabel dynamicment

Je souhaite resize la hauteur de la cellule en fonction de la hauteur de l'label et de la hauteur de l'label en fonction du text. Ou est-il possible de resize la taille de la cellule en fonction du text entré dans UITextView ?

Cette méthode est dépréciée depuis iOS 7.0.

Il existe une méthode déléguée UITableView appelée heightForRowAtIndexPath appelée avant de créer une cellule ou une table.

Vous pouvez utiliser le NSIndexPath passé pour get le text sur une ligne spécifique et utiliser la méthode UISsortingngDrawing.h de UISsortingngDrawing.h pour calculer une CGSize pour cette ligne.

Par exemple:

 CGSize size = [text sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; 

Et enfin, vous returnneriez size.height .

–Pour iOS7–

Basant ceci hors de la réponse de Josh Vera … place ceci dans heightForRowAtIndexPath.

J'ai mes données de table stockées dans un NSArray * tableData.

 // set the desired width of the textbox that will be holding the adjusted text, (for clarity only, set as property or ivar) CGFloat widthOfMyTexbox = 250.0; -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { //Get a reference to your ssortingng to base the cell size on. NSSsortingng *cellText = [self.tableData objectAtIndex:indexPath.row]; //set the desired size of your textbox CGSize constraint = CGSizeMake(widthOfMyTextBox, MAXFLOAT); //set your text atsortingbute dictionary NSDictionary *atsortingbutes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0] forKey:NSFontAtsortingbuteName]; //get the size of the text box CGRect textsize = [cellText boundingRectWithSize:constraint options:NSSsortingngDrawingUsesLineFragmentOrigin atsortingbutes:atsortingbutes context:nil]; //calculate your size float textHeight = textsize.size.height +20; //I have mine set for a minimum size textHeight = (textHeight < 50.0) ? 50.0 : textHeight; return textHeight; } 

Je ne l'ai pas testé pour iOS <7, mais je crois que cela devrait fonctionner aussi.