如何在heightForRowAtIndexPath中获取单元格?

时间:2015-10-20 07:49:16

标签: ios objective-c iphone uitableview ipad

我在我的app中创建了自定义单元格。我想在{ "took": 2, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 }, "hits": { "total": 3, "max_score": 0, "hits": [] }, "aggregations": { "collection": { "doc_count": 7, "keys": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "c", "doc_count": 2, "value_sum": { "value": 79 } }, { "key": "a", "doc_count": 3, "value_sum": { "value": 46 } }, { "key": "b", "doc_count": 1, "value_sum": { "value": 21 } }, { "key": "f", "doc_count": 1, "value_sum": { "value": 20 } } ] } } } } 中获取每个单元格。请告诉我如何在此方法中获取自定义单元格。我尝试过此代码,但这会导致无限循环&安培;终于崩溃了应用程序。

HeightForRowAtIndexPath

修改

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

HomeCell *cell=(HomeCell *)[tableView cellForRowAtIndexPath:indexPath];

请说出哪种方式最好?

5 个答案:

答案 0 :(得分:9)

  

如何在heightForRowAtIndexPath中获取单元格?

这是不可能的,因为当调用-heightForRowAtIndexPath时,尚未创建任何单元格。您需要了解UITableView的工作原理:

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

    -numberOfSectionsInTableView

    此时没有创建单元格。

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

    -numberOfRowsInSection

    此时没有创建单元格。

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

    -heightForRowAtIndexPath

    此时没有创建单元格。

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

    -cellForRowAtIndexPath

    此时创建了单元格。

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

    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,使用Autolayout

答案 1 :(得分: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;
}

答案 2 :(得分:0)

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

即可

或像cellForRowtableView.dequeueWithReuseIdentifier:)中所做的那样将其取名。

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

您可以做的是以下内容:

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

你应该做什么:

使用自动布局并添加所有必要的约束,所有标签都会动态调整大小。获得自动布局的基础知识大约需要3到4个小时,而且大约需要一个月的时间才能轻松掌握它。

我非常强烈建议您不要使用对象框进行大小调整,大多数标签和视图都会调整大小,如果您正确使用约束,则无需编写任何代码。

完成后,因为您拥有不同高度的单元格,所以使用tableview的DynamicHeight属性以及随附的轻微调整。你可以找到

A great tutorial here

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

This amazing StackOverflow answer which you MUST read

答案 3 :(得分:0)

我建议你在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 :(得分:-3)

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

Custom Cell * cell = [tableview cellForRowAtIndexPath:indexPath];

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

相关问题