将Dynamic Cell的表视图内容高度设置为表视图高度约束ios

Rav*_*mar 4 iphone objective-c nested-table ios autolayout

我有tableview(A)的每个自定义单元都有tableview(B)和动态表视图单元格.

在tableview(A)cellForRowAtIndex.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

     MainMessageTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MsgMainCell"];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSInteger index = indexPath.row;

        MessageMain *result = tableData[index];

        cell.dateLabelTC.text = [NSString stringWithFormat:@"Date : %@",result.createdTime];
        cell.subjectLabelTC.text = [NSString stringWithFormat:@"Subject : %@",result.subject];

        NSArray *arrList = result.messageList;
        [cell setupTableData:(NSMutableArray *)arrList];

        [cell setNeedsUpdateConstraints];
}
Run Code Online (Sandbox Code Playgroud)

在tableview(A)的自定义单元重载表视图(B).

-(void)setupTableData:(NSMutableArray *)tableData{

    _tableData = tableData;

    [self.tableView reloadData];
}
-(void)updateConstraints{
    [super updateConstraints];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView layoutIfNeeded];
        CGFloat height = self.tableView.contentSize.height;//+1000;
        tableBHeightConstraints.constant  = height;
    });
}
Run Code Online (Sandbox Code Playgroud)

tableBHeightConstraints是tableview(A)的单元格子表中表视图(B)的高度约束.tableBHeightConstraints.constant没有获得所有计算约束的正确值.

在动态表格单元格的高度设置之后,获取tableView.contentSize.height的最佳位置或方法是什么.

这是tableview(A)的Cell 在此输入图像描述

在此输入图像描述

这是tableview(B)的Cell

请帮帮我们

Sta*_*ses 5

在viewDidLoad方法中添加以下代码.

   self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView setEstimatedRowHeight:85.0];
Run Code Online (Sandbox Code Playgroud)

还包括estimatedHeightForRowAtIndexPath方法并返回估计的行高,如下所示,

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
Run Code Online (Sandbox Code Playgroud)

为heightForRowAtIndexPath指定自动维度.该方法向委托询问用于指定位置的行的高度.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
Run Code Online (Sandbox Code Playgroud)

注意

要使表格行具有动态高度,必须将内容视图中的标签固定到顶部和底部,以便它们可以根据内容缩小或增大.如果您遇到任何困难,请更新我以防万一.