Il existe de nombreuses solutions qui utilisent "sizeWithFont" ou quelque chose de similaire, qui est déconseillé à partir de iOS 7.
Voici un code que j'ai reconstitué jusqu'à présent. La hauteur change, mais pas du tout avec précision:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSsortingng *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... cell.textLabel.text = "The Cell's Text!"; cell.textLabel.numberOfLines = 0; [cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping]; [cell.textLabel sizeToFit]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; { CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGSize screenSize = screenBounds.size; NSAtsortingbutedSsortingng *aSsortingng = [[NSAtsortingbutedSsortingng alloc] initWithSsortingng:"The Cell's Text!"]; UITextView *calculationView = [[UITextView alloc] init]; [calculationView setAtsortingbutedText:aSsortingng]; CGSize size = [calculationView sizeThatFits:CGSizeMake(screenSize.width, FLT_MAX)]; return size.height; }
Pour un autre exemple, voici une réponse similaire : https://stackoverflow.com/a/9828777/693121 mais comme je l'ai déjà dit, cela utilise du code obsolète.
Vous devriez utiliser la méthode mentionnée dans les docs pour replace l'ancienne – boundingRectWithSize: options: atsortingbutes: context :. Voici un exemple que je pense devrait fonctionner (il fonctionne de toute façon avec des labels multi-lignes).
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSSsortingngDrawingContext *ctx = [NSSsortingngDrawingContext new]; NSAtsortingbutedSsortingng *aSsortingng = [[NSAtsortingbutedSsortingng alloc] initWithSsortingng:@"The Cell's Text!"]; UITextView *calculationView = [[UITextView alloc] init]; [calculationView setAtsortingbutedText:aSsortingng]; CGRect textRect = [calculationView.text boundingRectWithSize:self.view.frame.size options:NSSsortingngDrawingUsesLineFragmentOrigin atsortingbutes:@{NSFontAtsortingbuteName:calculationView.font} context:ctx]; return textRect.size.height; }
Cela suppose que vous voulez que la vue de text ait la taille de self.view. Si ce n'est pas le cas, vous devez utiliser initWithFrame pour votre vue de text et transmettre calculateView.frame.size pour le paramètre boundingRectWithSize:.
Ceci est la version la plus facile, iOS 7 fait tout le travail lourd (seulement pour les uitableviewcells basées sur autolayout):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Cell"]; [self configureCell:cell]; // edit: Need to call [cell layoutIfNeeded]; otherwise the layout changes wont be applied to the cell [cell layoutIfNeeded]; return [cell.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height; }
systemLayoutSizeFittingSize réévalue les contraintes de layout automatique et renvoie la hauteur parfaite pour votre cellule. Facile, non?
Je l'ai eu de mon collègue (ANIRUDH) et fonctionne bien, peut être utile:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { //Calculating height on base of length of text UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; NSAtsortingbutedSsortingng *atsortingbutedText = [[NSAtsortingbutedSsortingng alloc] initWithSsortingng:YOURSTRING //[NSSsortingng ssortingng withformat:@"\n Yourssortingng"] \n for: number of lines require for static label in cell atsortingbutes:@ { NSFontAtsortingbuteName: font }]; CGRect rect = [atsortingbutedText boundingRectWithSize:(CGSize){CELL_CONTENT_WIDTH, CGFLOAT_MAX} options:NSSsortingngDrawingUsesLineFragmentOrigin context:nil]; CGSize size = rect.size; // NSSsortingng *height = [NSSsortingng ssortingngWithFormat: @"%.2f", ceilf(size.height)+22]; return (ceilf(size.height)+22)*0.6; //change 0.6 to 0.9 according to the difference in required and extra calculted height, it works for me every time }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSSsortingng *yourText = [[resultArray objectAtIndex:indexPath.row] valueForKey:@"review_text"]; CGSize lblwidth = CGSizeMake(300, CGFLOAT_MAX); CGSize requiredSize = [yourText sizeWithFont:[UIFont fontWithName:@"CALIBRI" size:17] constrainedToSize:lblwidth lineBreakMode:NSLineBreakByWordWrapping]; int calculatedHeight = requiredSize.height+50; return (float)calculatedHeight; }
Pour get la taille de votre string, vous devez utiliser - (CGSize)sizeWithAtsortingbutes:(NSDictionary *)attars
.
Donc, pour votre exemple, vous devez calculer la hauteur de l'label de text en tant que telle:
CGSize *cellSize = [cell.textLabel.text sizeWithAtsortingbutes:@{NSFontAtsortingbuteName:[cell.textLabel.font]}];
ou
CGSize *rowSize = [aSsortingng sizeWithAtsortingbutes:nil];