2014-05-22 26 views
0

后调整高度的UITableViewCell我可以根据我的约束没有设定我UITableViewCell的高度。的cellForRowAtIndexPath

我在每个单元格上都有我的UITableView a UILabel其中高度不固定并取决于此高度的这个UILabel

我试图设置在框架上电池后,我更新标签的内容,但没有任何反应。请在下面找到我的代码:

//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; 

    } 
} 

我想也设置高度感谢heightForRowAtIndexPath方法,但这种方法cellForRowAtIndexPath在我的标签更新之前称为

我没有更多的想法来解决这个问题。

预先感谢您。

+1

没有魔法,你需要确定在'heightForRowAtIndexPath'方法你的身高和返回。你不能设置单元格的高度'cellForRowAtIndexPath' – Adam

回答

0

由于罗希特的回答,请找我的工作代码:

-(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

我建议不要使用像'234.00f'这样的硬编码值。特别是现在我们有了iPhone 6/6 Plus(但即使这个问题得到解答,我们也有iPad屏幕),这可能会产生意想不到的效果。如果你要这样的硬编码值,这是更好的'UITableViewCell'子类,你可以为不同的设备使用不同的硬编码值,这样做。 – 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

谢谢您的回答。顺便说一句,你如何设置'comment_text'的值?它是你的代码的变量吗? – BriceB

+0

plz查看更新回答......其简单字符串.. – Rohit

+0

好的。但是在'cellForRowAtIndexPath'方法之后给出'str11'的等价物**。这是我的问题。在调用'heightForRowAtIndexPath'后,我无法更新单元格的大小。 – BriceB

相关问题