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

Posted

技术标签:

【中文标题】UItableview 单元格的动态高度与 UItableview 单元格内的 UItableview【英文标题】:UItableview cell Dyanmic height with UItableview inside the UItableview cell 【发布时间】:2015-01-12 13:00:51 【问题描述】:

我要求在 tableview 中放置提要和图像列表,并且每个提要 cmet 也应该与提要一起显示。

为此,我在 UItableview 单元格中使用了 Inner uitableview。并且 cmets tableview 高度应该是动态的。如何根据 Comments tableview (Inner tableview) 的高度更新 Feeds tableview 高度。

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

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

【问题讨论】:

为什么不使用多个部分? «您不应该在 UIScrollView 对象中嵌入 UIWebView 或 UITableView 对象。如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能会混淆和错误处理。» source。由于 UITableView 是 UIScrollView 的子视图,因此这里适用此规则。 【参考方案1】:

正如 cmets 中提到的@Wain,最好的方法可能是在单元格中使用多个部分而不是UITableView。每个部分都可以是一个帖子,第一个单元格是“帖子”,后续单元格是 cmets。或者,如果您希望在滚动 cmets 时将“帖子”粘贴到屏幕顶部,则可以将“帖子”设置为部分标题。

如果这是不可能的(例如,如果您已经将部分用于其他目的),您可以做的是在构建主表之前计算“子表”的总高度并将这些值存储到用作主表的单元格高度。例如,如果您知道您的第一个帖子有 7 个 cmets,总共占用 300pt 的高度,以及一个 50pt 的帖子,您就知道您的单元格需要有 350pt 的高度。如果你确实走这条路,我仍然认为这不是最好的方法,请确保关闭子表上的滚动。这将降低滚动视图冲突的风险,从而使最终用户感到烦恼和困惑。

【讨论】:

【参考方案2】:

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

-(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.

【讨论】:

【参考方案3】:

我终于以这种方式解决了我的问题。

我选择了多个部分,而不是多个表格视图。我将提要消息/图像部分标题和 cmets 作为该部分的行。这项工作很棒。

@wain 感谢您的提示。这将对其他人有所帮助。

【讨论】:

以上是关于UItableview 单元格的动态高度与 UItableview 单元格内的 UItableview的主要内容,如果未能解决你的问题,请参考以下文章

UITableView 单元格的动态高度问题(Swift)

设置 tableviewcell 高度以显示具有动态高度单元格的完整内部/嵌套 uitableview

SnapKit - UITableView 高度不随单元格的动态内容而变化

UITableView“dequeueReusableCellWithIdentifier”导致基于单元格内容的动态单元格高度的单元格重叠?

如何强制 uitableview 调整单元格的大小(在停用高度约束后)?

UITableView imageView 复用其他单元格的图片