UItableview 单元格动态高度与 UItableview 单元格内的 UItableview

UItableview cell Dyanmic height with UItableview inside the UItableview cell

我要求在表格视图中放置提要和图像列表,并且每个提要评论也应与提要一起显示。

为此,我在 UItableview 单元格中使用了 Inner uitableview。并且评论表视图高度应该是 dynamic.How 我可以根据评论表视图(内部表视图)的高度更新 Feeds 表视图高度吗?

我附上的这张示例图片包含 10 条评论,但由于静态大小我只能显示一条评论,我试图获取评论表格视图内容大小高度并重新加载提要表格视图,这会导致令人作呕的行为。

谁能给我一个更好的方法。

正如评论中提到的 @Wain,最好的方法可能是在一个单元格中使用多个部分而不是 UITableView。每个部分可以是 post,第一个单元格是 "post",随后的单元格是评论。或者,您可以将 "post" 设置为 header 部分,如果您希望它在滚动评论时固定在屏幕顶部。

如果这不可能(例如,如果您已经将部分用于其他目的),您可以做的是在构建主要 table 之前计算 "sub-tables" 的总高度] 并存储这些值以用作主要 table 的单元格高度。例如,如果您知道您的第一个 post 有 7 个评论将占据 300pt 的总高度,以及 post 占 50pt,您就知道您的单元格需要 350pt 的高度。如果你确实选择了这条路线,我仍然认为这不是最好的方法,请确保关闭 sub-tables 上的滚动。这将降低滚动视图冲突的风险,这种冲突会使最终用户感到厌烦和困惑。

最好的办法是动态计算 table 视图中每个单元格的大小。我有一个要求,我必须做一个像 table 这样的 facebook 视图,其中的内容可能具有可变高度。尤其是文字。所以我试图在我的单元格 contentView 中找到所有可变大小内容所需的高度,并相应地设置单元格的高度。以下是一些代码片段:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"Cell";
    JHAlertDetailViewCell *cell = (JHAlertDetailViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"JHAlertDetailViewCell" owner:self options:nil];
        cell = [nibs objectAtIndex:0];

    }
    JHAlert *alert = [self.alerts objectAtIndex:indexPath.row];
    [self configureCell:cell withItem:alert];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self heightForBasicCellAtIndexPath:indexPath];
}

- (void)configureCell:(JHAlertDetailViewCell *)cell withItem:(JHAlert *)item{
    // In this method, you can actually populate all the contents of your cell and calculate the total     //height also

}

- (CGFloat)heightForBasicCellAtIndexPath:(NSIndexPath *)indexPath {
    static JHAlertDetailViewCell *sizingCell = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sizingCell = [self.alertsTV dequeueReusableCellWithIdentifier:@"JHAlertDetailViewCell"];
        if (sizingCell==nil) {
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"JHAlertDetailViewCell" owner:self options:Nil];
            sizingCell = [nib objectAtIndex:0];

        }


    });

    [self configureCell:sizingCell withItem:[self.alerts objectAtIndex:indexPath.row]];

    return sizingCell.whiteView.bounds.size.height+20; // Here, whiteView is the UIOutlet of the main background view of the cell. Its height as calculated in the configureCell: method is used here to return the required height of the cell.
}

最后我用这种方式解决了我的问题

我没有使用多个表格视图,而是选择了多个 sections.I 保留 Feed Message/image 部分 header 并将评论作为行 section.That 工作很棒。

@wain 谢谢你 hint.This 会帮助其他人。