我在我的应用程序中创建了自定义单元格。我想要获取HeightForRowAtIndexPath
中的每个单元格。请告诉我如何在此方法中获得自定义单元格。我试过这段代码,但这会导致无限循环&终于会崩溃应用程序。如何在heightForRowAtIndexPath中获取单元格?
HomeCell *cell=(HomeCell *)[tableView cellForRowAtIndexPath:indexPath];
编辑:
我曾经试过,但它给了我细胞高度为零。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"HomeCell";
HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
float tv_view_height=cell.tv_post.frame.size.height;
float like_count_height=cell.label_like_count.frame.size.height;
float first_comment_height=cell.first_comment.frame.size.height;
float second_comment_height=cell.second_cmment.frame.size.height;
float third_comment_height=cell.third_comment.frame.size.height;
Post *user_post=[arr_post objectAtIndex:indexPath.row];
float comment_count=[user_post.comment_count intValue];
if(comment_count<=0)
{
first_comment_height=0;
second_comment_height=0;
third_comment_height=0;
}
else if(comment_count==1)
{
second_comment_height=0;
third_comment_height=0;
}
else if(comment_count==2)
{
third_comment_height=0;
}
float like_count=[user_post.like_count intValue];
if(like_count<=0)
{
like_count_height=0;
}
float total_height=tv_view_height+like_count_height+first_comment_height+second_comment_height+third_comment_height;
NSLog(@"total heigh is %f'",total_height);
return total_height;
}
请告诉哪个是最好的方法?
告诉我你要做什么? – jigs
在'heightForRowAtIndexPath'里面使用'dequeueReusableCellWithIdentifier'不是一个好主意。 – 0yeoj
你建议一些更好的plaese。 – Techiee