I have a table view that contains a static set of 2 rows (created in storyboard). I have a specific UITableViewCell that contains a text box thats size is dynamically generated. I would like to also dynamically set the hight for the containing UITableViewCell, however setting the .frame for the UITableViewCell does not seem to be working. Has anyone done this programatically, without using tableView:heightForRowAtIndexPath: ?
Current code:
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [self.place.details sizeWithFont:self.descriptionLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:self.descriptionLabel.lineBreakMode];
CGRect labelFrame = self.descriptionLabel.frame;
labelFrame.size.height = expectedLabelSize.height;
// this does not change the height of the UITableViewCell....
self.descriptionTableViewCell.frame = labelFrame;
As you’ve noticed regardless of the height of the
UITableViewCell, it is clipped to the height of theUITableView‘s row for that index path. By the timetableView:cellForRowAtIndexPath:is called the row heights have already been laid out. Just usetableView:heightForRowAtIndexPath:as this is what it is intended for. A key point is that the table view needs to know the height of all its rows to set up its scroll view, scroll bars, etc. On the other hand it only needs to have the actual cells for what is on screen. See this answer for more discussion on why the UITableViewDelegate works this way.