简体   繁体   English

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

[英]Set Dynamic Cell's Table View Content height to table view height constraints ios

I have tableview(A)'s every custom cell having tableview(B) with dynamic table view cell. 我有tableview(A)的每个自定义单元都有tableview(B)和动态表视图单元格。

At tableview(A) cellForRowAtIndex. 在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];
}

At tableview(A)'s custom cell reload tableview(B). 在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;
    });
}

tableBHeightConstraints is height constraints of table view(B) in tableview(A)'s cell's child. tableBHeightConstraints是tableview(A)的单元格子表中表视图(B)的高度约束。 tableBHeightConstraints.constant not getting correct value with all calculate constraints. tableBHeightConstraints.constant没有获得所有计算约束的正确值。

what is the best place or method to get tableView.contentSize.height exact after dynamic table cell's height set. 在动态表格单元格的高度设置之后,获得tableView.contentSize.height的最佳位置或方法是什么。

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

在此输入图像描述

This is tableview(B)'s Cell 这是tableview(B)的Cell

Please Help guys. 请帮帮我们

Add the following code in you viewDidLoad method. 在viewDidLoad方法中添加以下代码。

   self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView setEstimatedRowHeight:85.0];

Also include the estimatedHeightForRowAtIndexPath method and return a estimated row height as follows, 还包括estimatedHeightForRowAtIndexPath方法并返回估计的行高,如下所示,

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

Assign an automatic dimension for heightForRowAtIndexPath.The method asks the delegate for the height to use for a row in a specified location. 为heightForRowAtIndexPath指定自动维度。该方法向委托询问用于指定位置的行的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

NOTE 注意

For your table row to be of dynamic height, the labels within the contentview has to be pinned to the top and bottom so that they can shrink or grow as per the contents. 要使表格行具有动态高度,必须将内容视图中的标签固定到顶部和底部,以便它们可以根据内容缩小或增大。 Do update me in case if you are facing any difficulty. 如果您遇到任何困难,请更新我以防万一。

Try This : 试试这个 :

take a height constraint of UItableView 采用UItableView的高度约束

_tableViewHieghtConstraint.constant = height of row *count of array. _tableViewHieghtConstraint.constant =行的高度*数组的计数。

in tableview number of section method. 在tableview中的节数方法。

If you can't get this to work, then why not try something different. 如果你不能让它工作,那么为什么不尝试不同的东西。 Instead of trying to stick a tableView in a tableCell, why not just modify your datasource to govern the two tables as one. 为什么不修改数据源来管理两个表,而不是试图将tableView固定在tableCell中。 That is probably a better solution as is should be more efficient and work better as a single table. 这可能是一个更好的解决方案,因为它应该更高效,并且作为单个表更好地工作。

If you use grouped style, then effectively each group is an individual tableView with a header and footer. 如果使用分组样式,则实际上每个组都是具有页眉和页脚的单个tableView。

It's better to simply set the constraint to equal height: 最好将约束设置为相等的高度:

tableA_cell.contentView height = TableB height

With visual constraints it would be something like this: 有了视觉约束,它将是这样的:

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(tableB);
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V|[tableB|" options:0 metrics:0 views:viewsDictionary];

Then the magic should happen by itself. 然后魔术应该自己发生。

If you make any changes to tableB, just call 如果对tableB进行任何更改,只需调用即可

[self.tableView beginUpdates]
[self.tableView endUpdates]

on table A to automatically update the height of it's cells 在表A上自动更新它的单元格的高度

Try moving your changes from -(void) updateConstraints into -(void) viewDidLayoutSubviews . 尝试将您的更改从-(void) updateConstraints-(void) viewDidLayoutSubviews

-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM