简体   繁体   中英

Add UITableView inside UITableViewCell with automatic height

I want to display UITableView inside a UITableViewCell. The table view inside the table view cell may have a random count of the cell so the cell height should be table view content size height

I added constraints and here is my height for a row at index path

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

in your InnerCellModel make a method.

-(CGFloat)cellHeight {
    CGFloat height = [self makeSomeMethodToCalculateHeightOfText:self.title withFont: yourLabelFont, andWidth: yourLabelWidth];
    height += someMarginIfAvailable;
    return height;
}

then iterate your array and add height of each index in a single variable and then assign it to your innerTableViewHeightConstraint .

Set some Height Constraint to your inner UITableView and then in the cellForRowAt indexPath after [innerTableView reload] and then set

innerTableViewHeightConstraint.constant = innerTableView.contentSize.height;
// Or You can even calculate your inner height and then set it to your tableView.
innerTableViewHeightConstraint.constant = numberOfCells * innerCellHeight;
[cell layoutIfNeeded];

PS: Your Cell height must be UITableViewAutomaticDimension; Inner TableView Must have Top, Left, Right, Bottom and Height Constraints as well.

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