2014-05-22 2 views
0

내 제한에 따라 내 UITableViewCell 높이를 설정할 수 없습니다.cellForRowAtIndexPath 다음에 높이 조정 UITableViewCell

각 셀에 내 UITableViewUILabel 높이가 고정되어 있지 않으며 높이가이 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

감사 Rohit의 대답에, 내 작업 코드를 발견하십시오

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

Ok. 그러나'str11'과 동등한 것은 ** cellForRowAtIndexPath' 메소드 후에 ** 주어진다. 그게 내 문제 야. 'heightForRowAtIndexPath'가 호출 된 후에 셀의 크기를 업데이트 할 수 없습니다. – BriceB

관련 문제