如何动态更改自定义单元格的表格视图单元格的高度?

Posted

技术标签:

【中文标题】如何动态更改自定义单元格的表格视图单元格的高度?【英文标题】:how to dynamically change the height of tableview cell for custom cell? 【发布时间】:2015-10-27 09:51:05 【问题描述】:

我创建了一个自定义单元格表视图。这个自定义单元格包含标签,所以我从情节提要添加了标签,并从服务器显示了它们的数据。所以如果标签没有数据,那么我隐藏标签,但如果有是数据,然后我用服务器数据显示标签。因此,如果标签上有数据,则单元格的高度可以说 180,因为标签始终存在。现在,当标签没有数据时,我想删除或隐藏标签并降低单元格的高度,因为所有标签都是垂直显示的。这样我想计算单元格的高度。

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;

【问题讨论】:

你能告诉我你项目的部署目标吗? 您是否在代码中使用了自动布局?请提供您的代码,以便我解决您的问题。 ios 9 是目标并使用自动布局 【参考方案1】:
- (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;


【讨论】:

是的,您只需计算所有子视图包含的高度并组合所有并返回。将字符串值传递给 heigtForCellwithString 并获取大小。 但在这种情况下我没有得到合适的高度,这就是问题所在 我已经做到了.. 我也完美地完成了 10 种不同的现场工作......在调用高度方法之前,首先检查字段值是否为空。 bcoz 如果我们将空字符串传递给 height 方法,那么它会返回 20 hts。【参考方案2】:

如果您使用自动布局来调整表格单元格或任何视图的大小,更好的方法是更新视图的NSLayoutConstraint。

要更新表格视图单元格,请参考示例教程:http://derpturkey.com/autosize-uitableviewcell-height-programmatically/

【讨论】:

以上是关于如何动态更改自定义单元格的表格视图单元格的高度?的主要内容,如果未能解决你的问题,请参考以下文章

故事板中的自定义单元格行高度设置没有响应

使用自动布局自定义集合视图单元格的动态高度

当按钮是iOS中自定义单元格的子视图时,如何动态更改按钮文本?

动态表格视图单元格,以编程方式更改子视图高度

动态更改表格视图单元格的高度

制作自定义的动态表格视图单元格