【发布时间】: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 是目标并使用自动布局
标签: ios objective-c iphone uitableview ipad