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

时间:2015-10-27 09:51:05

标签: ios objective-c iphone uitableview ipad

我创建了一个自定义单元格表视图。此自定义单元格包含标签,因此我添加了标题为storyboard&我从服务器上显示它们的数据。如果没有标签的数据,那么我隐藏标签,但如果有数据,那么我显示带有服务器数据的标签。所以,如果有数据,则保留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;
}

2 个答案:

答案 0 :(得分:0)

"/sensor-data.php?action=csv_data&id='AB'&period=24"

答案 1 :(得分:0)

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

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

相关问题