2015-10-27 188 views
0

我已经创建了一个自定义单元格表视图。这个自定义单元格包含标签,所以我添加了标签从故事板&我显示他们从服务器上的数据。所以如果没有数据的标签,然后我隐藏标签,但如果有数据然后我显示标签与服务器data.So高度的细胞仍然让我们说180如果有标签上的数据或不标签,因为标签始终保持there.Now当没有数据的标签,然后我想删除或隐藏标签&减少单元格的高度,因为所有标签都垂直显示。这样我就可以计算单元格的高度。如何动态更改自定义单元格的tableview单元格的高度?

与HeightForAtIndex

  • 问题如何进入细胞的heightForRow?
  • 什么代码写我在heightForRowAtIndex获取单元格?

编辑:

我曾尝试这样的代码:

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

     float like_height,c_one_height,c_two_height,c_three_height,tv_height,header_height,operations_height; 
    //define variables here 
    if(!self.customCell) 
    { 
     self.customCell = [self.table_view dequeueReusableCellWithIdentifier:@"homeCell"]; 
    } 
    Post *user_post=[arr_post objectAtIndex:indexPath.row]; 
    int like_count=[user_post.like_count intValue]; 
    float comment_count=[user_post.comment_count intValue]; 
    [self.customCell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    if(like_count>0) 
    { 
     like_height=self.customCell.label_like_count.frame.size.height; 

    } 
    else 
    { 
     like_height=0; 
    } 
    if(comment_count<=0) 
    { 
     c_one_height=0; 
     c_two_height=0; 
     c_three_height=0; 

    } 
    else if(comment_count==1) 
    { 
     c_one_height=self.customCell.first_comment.frame.size.height; 
     c_two_height=0; 
     c_three_height=0; 

    } 
    else if(comment_count==2) 
    { 
     c_one_height=self.customCell.first_comment.frame.size.height; 
     c_two_height=self.customCell.second_cmment.frame.size.height; 
     c_three_height=0; 
    } 
    else if(comment_count==3) 
    { 
     c_one_height=self.customCell.first_comment.frame.size.height; 
     c_two_height=self.customCell.second_cmment.frame.size.height; 
     c_three_height=self.customCell.third_comment.frame.size.height; 
    } 
    tv_height=self.customCell.view_tvContainer.frame.size.height; 
    header_height=self.customCell.header_view_height.frame.size.height; 
    operations_height=self.customCell.view_operations_height.frame.size.height; 
    CGFloat height = like_height+c_one_height+c_two_height+c_three_height+tv_height+header_height+operations_height; 
    // Padding of 1 point (cell separator) 
    CGFloat separatorHeight = 1; 
    return height + separatorHeight; 
} 
+0

你能告诉我你的项目的部署目标是什么? – KavyaKavita

+0

你在代码中使用autolayout吗?并请提供您的代码,以便我可以解决您的问题。 – riddhi

+0

ios 9是目标&使用自动布局 – Techiee

回答

0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
NSString *text=[InformationArray objectAtIndex:indexPath.row]; 
    UIFont *font = [UIFont fontWithName:@"HelveticaNeue" size:16.5]; 
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    CGSize labelHeight = [self heigtForCellwithString:text withFont:font]; 

return labelHeight.height +10; 

} 
//calculate the height 
-(CGSize)heigtForCellwithString:(NSString *)stringValue withFont:(UIFont*)font{ 

CGSize constraint = CGSizeMake(250,9999); 
    NSDictionary *attributes = @{NSFontAttributeName: font}; 
    CGRect rect = [stringValue boundingRectWithSize:constraint options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil]; 

    return rect.size; 

} 
+0

请再次看到我的问题。我有很多查看里面的 – Techiee

+0

是的,你只是计算所有的子视图包含高度,并结合所有和返回。将字符串值传递给heigtForCellwithString并获取大小。 –

+0

但我没有得到正确的高度,在这种情况下,这是问题 – Techiee

相关问题