0
votes

I have a UITableView inside a UITableViewCell pinned on all four sides. I want to change the height of UITableView based on number of cells it has. For that I have subclassed UITableViewCell (name- TableCell). Inside it, I am using this code to update the tableView height.:

-(void)layoutSubviews{

    [super layoutSubviews];

    if ([self.data count] <= 5) {
        self.tableHeightConstraint.constant = ([self.data count]) * 44.0f;
    } else {
        self.tableHeightConstraint.constant = 5 * 44.0f;
    }
}

I change the height constraint of TableView above dynamically. It works sometimes but other times leads to breaking constraints leading to jittery behaviour of app.

Is this the correct way of changing height? Please help!

Edit 1:

I am inserting and deleting cells at runtime. The inserted cell has a tableview whose size is dynamic based on number of cells. When I insert (while including the above code) the insertion is correct. But when I delete, the cell (having uitableview) is still visible. When I scroll the cell out of screen, then only it goes invisible. Kindly help!

1

1 Answers

1
votes

if you have tableview inside tableViewCell that is pinned to all sides of the cell (bottom,left,right,top) and it doesn't have a high content hugging priority you can set the height of the cell in the outer tableView in:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

the inner tableview will be stretched since it have pinned constraints