【发布时间】:2015-10-20 07:49:16
【问题描述】:
我在我的应用程序中创建了自定义单元格。我想获取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;
}
请告诉哪个是最好的方法?
【问题讨论】:
-
告诉我你想做什么??
-
在
heightForRowAtIndexPath中使用dequeueReusableCellWithIdentifier不是一个好主意。 -
你建议一些更好的plaese。
-
你能告诉我们你想要达到的目标吗.. 你想在 heightForRowAtIndexPath 中获取单元格.. ??
-
同意@0yeoj,您可以尝试使用
indexPath.row作为NSDictionary中的键来跟踪- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中单元格的高度,然后获取heightForRowAtIndexPath 中的高度
标签: ios objective-c iphone uitableview ipad