我已经创建了一个自定义单元格表视图。这个自定义单元格包含标签,所以我添加了标签从故事板&我显示他们从服务器上的数据。所以如果没有数据的标签,然后我隐藏标签,但如果有数据然后我显示标签与服务器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;
}
你能告诉我你的项目的部署目标是什么? – KavyaKavita
你在代码中使用autolayout吗?并请提供您的代码,以便我可以解决您的问题。 – riddhi
ios 9是目标&使用自动布局 – Techiee