4

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 enter image description here

enter image description here

This is tableview(B)'s Cell

Please Help guys.

Ravi Kumar
  • 1,356
  • 14
  • 22

5 Answers5

3

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.

  • I have already done this into my code for table viewA and tableviewB also. tableviewB's height show increase as its cell height increase u due to data feed as also tableviewA cell height also increase. – Ravi Kumar Jun 18 '16 at 09:35
  • can you please explain further –  Jun 18 '16 at 09:37
  • if tableviewB's cell height increase due to text set into label (you can see attach image last one).then height of tableviewB should increase. that is inside tableviewA's cell so tableviewA's cell should also increase. its a nest table view in another tableview's cell. – Ravi Kumar Jun 18 '16 at 09:40
1

Try This :

take a height constraint of UItableView

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

in tableview number of section method.

Arun sharma
  • 642
  • 1
  • 7
  • 17
1

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.

markt
  • 903
  • 7
  • 21
  • ya I don't have choice. I have to go done as you said thank. problem with group style I can not able to set background image in every group. I have to set image in every header,cell and footer. – Ravi Kumar Jun 20 '16 at 09:28
0

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

markt
  • 903
  • 7
  • 21
  • t's better to simply set the constraint to equal height: tableA_cell.contentView height = TableB height its a good idea but how can do this. can you please give me idea, I am using constraints. and what is the best place to set tableviewB's height – Ravi Kumar Jun 18 '16 at 09:06
  • See my updated answer. There is not really enough code in your question answer this any more. There is no code showing where you create tableB or set it's constraints. – markt Jun 18 '16 at 09:16
  • Sorry, I can't help you with that one. I do constraints with code. I don't understand the wysiwyg tools. Maybe you need to add some constraints with code as well as I have suggested above. The other thing you can do is implement - tableView:heightForRowAtIndexPath: in tableA's UITableViewDelegate to return the actual calculated height instead of automatic dimension. – markt Jun 18 '16 at 11:05
  • I just what to implement nested tableview using autolayout. dynamic tableview in another view cell. Please did tried many ways but not getting solutions. Please help me out. – Ravi Kumar Jun 19 '16 at 13:20
0

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;
});

}