Je ne peux pas définir la hauteur de mon UITableViewCell
en fonction de mes contraintes.Ajuster la hauteur UITableViewCell après cellForRowAtIndexPath
J'ai dans chaque cellule sur mon UITableView
un UILabel
où la hauteur n'est pas fixe et dépend de la hauteur de ce UILabel
.
J'ai essayé de définir le cadre sur la cellule après avoir mis à jour le contenu de l'étiquette, mais rien ne se passe. S'il vous plaît trouver mon code ci-dessous:
//On my controller
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CommentCell";
CommentCell *commentCell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
if (commentCell == nil) {
commentCell = [[CommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Comment *comment;
if (commentsArray.count > indexPath.row) {
comment = [commentsArray objectAtIndex:indexPath.row];
}
[commentCell setComment:comment];
[commentCell setCommonFields];
currentCellheight = commentCell.contentLabel.frame.size.height + 40;
DDLogInfo(@"Current Cell Height: %f", currentCellheight);
return commentCell;
}
//On my custom Cell (CommentCell.m)
-(void)setCommonFields {
if (self.comment != nil) {
self.contentLabel.text = self.comment.content;
self.contentLabel.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(self.contentLabel.frame.size.width, FLT_MAX);
CGRect expectedLabelRect = [self.contentLabel.text boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
CGRect newFrame = self.contentLabel.frame;
newFrame.size.height = expectedLabelRect.size.height;
self.contentLabel.frame = newFrame;
}
}
J'ai aussi essayé de régler la hauteur grâce à la méthode heightForRowAtIndexPath
, mais cette méthode est appelée avantcellForRowAtIndexPath
où mon label est mis à jour.
Je n'ai pas beaucoup plus d'idée pour résoudre ce problème.
Merci d'avance.
Pas de magie, vous devez déterminer votre taille dans la méthode 'heightForRowAtIndexPath' et le retourner. Vous ne pouvez pas définir la hauteur de la cellule dans 'cellForRowAtIndexPath' – Adam