简体   繁体   中英

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.

At 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).

-(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.constant not getting correct value with all calculate constraints.

what is the best place or method to get tableView.contentSize.height exact after dynamic table cell's height set.

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

在此输入图像描述

This is tableview(B)'s Cell

Please Help guys.

Add the following code in you viewDidLoad method.

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

Also include the estimatedHeightForRowAtIndexPath method and return a estimated row height as follows,

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

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

_tableViewHieghtConstraint.constant = height of row *count of array.

in tableview number of section method.

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

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

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

on table A to automatically update the height of it's cells

Try moving your changes from -(void) updateConstraints into -(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;
});

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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