强制UITableView在显示之前加载单元格

时间:2016-06-02 02:59:13

标签: ios iphone uitableview

我需要强制UITableView根据数据源加载所有单元格,并在UI上显示之前得到UITableView高度。我还有其他基于该高度的逻辑。我的行具有动态高度,单元格内的内容是自动覆盖的。这就是为什么我不能手动计算(或不知道如何)。在我的情况下,在显示UITableView之前获得高度至关重要。

请告知。

1 个答案:

答案 0 :(得分:0)

现在,要求是找到填充数据的单元格高度

这样,您可以在tableView

上显示之前找到单元格的高度

是的,您可以使用以下代码执行此操作:

/****/
- (CGFloat)heightForBasicCellAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier= @"IDENTIFIER";
    static dispatch_once_t onceToken;

    static UITableViewCell *cell = nil;

    dispatch_once(&onceToken, ^{
        cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    });

    /**SET ALL DATA AND VALUES ON CELL*/

    CGFloat height = [self calculateHeightForConfiguredSizingCell:cell];
//    NSLog(@"height at indexPath  %ld --> %f",(long)indexPath.row, height);

    return height;

}

- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell {
    [sizingCell setNeedsLayout];
    [sizingCell layoutIfNeeded];

    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    CGFloat height = size.height;
    return height + 1.0f; // Add 1.0f for the cell separator height
}