如何在heightForRowAtIndexPath中获取单元格?

Tec*_*ain 2 iphone objective-c uitableview ipad ios

我已经在我的应用程序中创建了自定义单元格.我想要获取每个单元格HeightForRowAtIndexPath.请告诉我如何在此方法中获取自定义单元格.我已尝试此代码但这会导致无限循环并最终导致应用程序崩溃.

HomeCell *cell=(HomeCell *)[tableView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

编辑:

我试过这个,但它给我的单元格高度为零.

-   (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;
}
Run Code Online (Sandbox Code Playgroud)

请告诉我哪个是最好的方法?

ifa*_*fau 12

如何在heightForRowAtIndexPath中获取单元格?

这是不可能的,因为在-heightForRowAtIndexPath调用时,还没有创建单元格.你需要了解它的UITableView工作原理:

  1. UITableView询问它的数据源它将拥有多少部分

    -numberOfSectionsInTableView

    此时没有创建单元格.

  2. UITableView询问它的数据源每个部分将有多少行

    -numberOfRowsInSection

    此时没有创建单元格.

  3. UITableView询问每个可见行的委托高度,以了解单元格的位置

    -heightForRowAtIndexPath

    此时没有创建单元格.

  4. UITableView要求它的数据源为它提供一个在给定索引路径下显示的单元格

    -cellForRowAtIndexPath

    此时创建单元格.

您可以从数据模型计算每个单元格的高度.你不需要单元格 - 你已经知道包含评论的框架宽度,你知道它的内容,你知道它的字体,你知道换行模式等等.所以,你可以计算高度.例如:

CGFloat commentsHeight = 0;
Post *user_post = [arr_post objectAtIndex:indexPath.row];

for (NSString *comment in user_post.comments)
{
    CGRect commentrect = [comment boundingRectWithSize:CGSizeMake(self.view.bounds.size.width - 18, FLT_MAX)
                                          options:(NSStringDrawingUsesLineFragmentOrigin)
                                        attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}
                                          context:nil];
    commentsHeight += commentrect.size.height;
}
Run Code Online (Sandbox Code Playgroud)

您可以从其数据模型计算单元格其他组件的高度.

但现在,在2015年,这不是最好的方式.你真的会更好地阅读显示@Zil的教程,并使用Autolayout来完成.


Gil*_*and 0

您可以从头开始创建一个新单元,只需通过HomeCell *sexyCell = [[HomeCell alloc]init];

cellForRow 或者像您在( )中所做的那样将其出队tableView.dequeueWithReuseIdentifier:

尽管我建议从头开始创建一个并在之后处理它(将其设置为 nil),因为如果您将其出队,它们将进入队列并导致严重的内存泄漏,并最终导致同一索引路径有许多单元。

您可以执行以下操作:

  • 使用 alloc init 创建一个单元
  • 填写真实数据
  • 在其视图上使用 .layoutsubviews
  • 计算它的大小并将其应用到您的真实单元格

你应该做什么:

使用自动布局并添加所有必要的约束,所有标签都将动态调整大小。大约需要 3 到 4 个小时来掌握自动布局的基础知识,大约需要一个月的常规使用才能真正轻松掌握它的窍门。

我强烈建议您不要使用对象框架调整大小,如果正确使用约束,大多数标签和视图都会像它们应该的那样调整大小,而无需编写任何代码。

完成此操作后,因为您有不同高度的单元格,所以需要使用 tableview 的 DynamicHeight 属性以及随之而来的细微调整。你可以找到

这里有一个很棒的教程

与 swift 相同的更新教程(更最新,但您需要翻译)

这个令人惊叹的 StackOverflow 答案你必须阅读