【问题标题】:How to get cell in heightForRowAtIndexPath?如何在 heightForRowAtIndexPath 中获取单元格?
【发布时间】: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


【解决方案1】:

如何获取 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;
}

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

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

【讨论】:

    【解决方案2】:

    您应该在 cellForRowAtIndexPath 中声明一个用于存储 TableView 单元格的数组,并且您可以在 heightForRowAtIndexPath 中使用存储的单元格。让我们尝试使用它。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         static NSString *cellIdentifier = @"HomeCellID";
    
         HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
         if (!cell) {
              cell = [[[HomeCell alloc] init] autorelease];
         }
    
         // Store table view cells in an array
         if (![tableViewCells containsObject:cell]) {
              [tableViewCells addObject:cell];
         }
    
         return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         if([tableViewCellsArray objectAtIndex:indexPath.row]) {
              HomeCell *cell = (HomeCell *)[tableViewCells objectAtIndex:indexPath.row];
              // Process your Code
         }
    
         return yourCalculatedCellHeight;
    }
    

    【讨论】:

      【解决方案3】:

      我建议您在 viewController 上将高度作为配置集合。

      类似这样的:

      - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          CGFloat height;
          CellConfiguration * selectedCellConfiguration =[_cellConfigurations objectAtIndex:indexPath.row];
      
          switch (selectedCellConfiguration.type) {
              case TheTypeYouNeed:
                return TheValueYouNeed
              default:
                  height = 44.0f;
                  break;
          }
      
          return height;
      }
      

      【讨论】:

        【解决方案4】:

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

        或者像你在 cellForRow (tableView.dequeueWithReuseIdentifier:) 中所做的那样让一个队列出队。

        虽然我建议从头开始创建一个并在(将其设置为 nil)之后处理它,因为如果您将其出列到那里,它们将进入队列并导致严重的内存泄漏并最终导致很多相同 indexPath 的单元格。

        你可以做的是:

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

        你应该做什么:

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

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

        一旦你这样做了,因为你有不同高度的单元格,正在使用 tableview 的 DynamicHeight 属性和它附带的轻微调整。你可以找到

        A great tutorial here

        The same updated tutorial for swift(更新更多,但您需要翻译)

        This amazing StackOverflow answer which you MUST read

        【讨论】:

        • 我已经尝试过了,但这给了我错误的结果。就像第 4 个单元格的图像显示在第一个 cel 上一样。
        • 您想要在 heightForRow 中创建一个单元格的唯一原因是对高度进行一些计算,例如动画。如果你得到错误的数据是因为你的 cellForRow:。除非你在 heightFow 中使用 dequeue,否则会导致问题。
        • 其实只是把你的两个方法(cellforrow 和 heightforrow)贴出来,这样我就可以看到完整的代码了
        • 查看我编辑的答案。这将花费你一些时间,但我真的强烈建议你花几天时间。这是正确的道路,也是一种很好的做法。
        【解决方案5】:

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexpath.row inSection:0];

        自定义单元格 *cell = [tableview cellForRowAtIndexPath:indexPath];

        这样,您将获得方法中的每个单元格

        【讨论】:

        • 这将无限循环
        • 这样您就可以获取每个单元格,然后您必须返回一个单元格高度。并且您的应用在返回后不会崩溃。
        • 你用过 HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; ,上面这行,如果是,则将其删除。因为你不应该在 heightForRowAtIndexPath 中重用你的 cel
        • 还要注意,heightForRowAtIndexPath 方法会在 cellForRowAtIndexPath 方法之前调用
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-22
        • 1970-01-01
        • 1970-01-01
        • 2012-08-08
        • 1970-01-01
        • 2023-03-02
        • 1970-01-01
        相关资源
        最近更新 更多