2014-05-22 2 views
0

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.

+1

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

Répondre

0

Merci à la réponse de Rohit, s'il vous plaît trouver mon code de travail:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *content=((Comment *)[commentsArray objectAtIndex:indexPath.row]).content; 
    CGSize maximumLabelSize = CGSizeMake(234.00f, FLT_MAX); 
    CGRect expectedLabelRect = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil]; 

    if (expectedLabelRect.size.height > 41) { 
     return expectedLabelRect.size.height + 81; 
    } 
    else 
     return 81; 
} 
+0

Je recommande d'utiliser des valeurs codées en dur comme '234.00f'. Surtout maintenant que nous avons l'iPhone 6/6 Plus (mais même lorsque cela a été répondu, nous avions des écrans iPad), cela produira probablement des effets inattendus. Si vous voulez coder en dur des valeurs comme celles-ci, il est préférable de le faire dans la sous-classe 'UITableViewCell' où vous pouvez utiliser différentes valeurs codées en dur pour différents périphériques. – mbm29414

0
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 


       NSLog(@"indexPath.row :%ld",(long)indexPath.row); 

       CGSize contsize={260.00f,3000.00f}; 
       UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:15.0]; 

       NSString *[email protected]"hellllllooooooooooooooooooo hellllllooooooooooooooooooo hellllllooooooooooooooooooo"; 

       CGSize fontSize=[strt11 sizeWithFont:font constrainedToSize:contsize lineBreakMode:NSLineBreakByWordWrapping]; 

       NSLog(@"fontSize indexPath :%f",fontSize.height); 

       if (fontSize.height>22) 
        return fontSize.height+50.0f; 

      } 




    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 


      UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuse"]; 

      if(cell==nil) 
      { 

       cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"]; 



       UILabel *lblcomment=[[UILabel alloc] initWithFrame:CGRectMake(55,23, 160,20)]; 
       [lblcomment setBackgroundColor:[UIColor clearColor]]; 
       [lblcomment setTag:72]; 
       [lblcomment setTextColor:[UIColor grayColor]]; 
       [lblcomment setNumberOfLines:0]; 
       [lblcomment setFont:[UIFont fontWithName:@"Helvetica" size:15.0]]; 
       [lblcomment setText:@"Its rude day on goin...Its rude day on goin...Its rude day on goin..."]; 

       [cell addSubview:lblcomment]; 
      } 
      UILabel *lblcomment=(UILabel *)[cell viewWithTag:72]; 



      CGSize contsize={260.00f,3000.00f}; 
      UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:15.0]; 

      NSString *[email protected]"hellllllooooooooooooooooooo hellllllooooooooooooooooooo hellllllooooooooooooooooooo"; 

      CGSize fontSize=[strt11 sizeWithFont:font constrainedToSize:contsize lineBreakMode:NSLineBreakByWordWrapping]; 

      float h=20; 
      NSLog(@"re %f",fontSize.height); 

      if (fontSize.height>22.0f){ 
       h=fontSize.height+7.0; 
       NSLog(@"heigtaaaaa %f",h); 
      } 

      [lblcomment setFrame:CGRectMake(55,23,260,h)]; 

      NSString *[email protected]""hellllllooooooooooooooooooo hellllllooooooooooooooooooo hellllllooooooooooooooooooo"; 




      [lblcomment setText:strDecodeComment]; 



      [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
      return cell; 
    } 
+0

Merci pour votre réponse. Au fait, comment définissez-vous la valeur de 'comment_text'? Est-ce une variable sur votre code? – BriceB

+0

plz voir la mise à jour réponse ...... sa chaîne simple .. – Rohit

+0

Ok. Mais l'équivalent de la 'str11' est donné ** après ** la méthode' cellForRowAtIndexPath'. C'est mon problème. Je ne peux pas mettre à jour la taille de la cellule après que le 'heightForRowAtIndexPath' a été appelé. – BriceB

Questions connexes