0
votes

I have a UIViewController with a UITableView, and each UITableViewCell has a UICollectionView inside it. The view controller is loaded from a basic simple nib file (a UIView with a UITableView inside it), and the custom UITableViewCell is loaded from a separate nib file. Both the UICollectionView and the UITableViewCell have to be dynamically sized. It works, but when the tableview and it's cells are loaded initially, they doesn't seem to have the right frame size when cellForRowAtIndexPath is called, which causes the collectionView to have an inaccurate size, and hence the UITableViewCell has a height that doesn't fit it's contents. Inside cellForRowAtIndexPath, it seems like it's taking the frame of the tableview as it shows up in the nib file, not what the correct size of the tableView should be on that device. If I call [tableView reloadData] or scroll up and down, the cell's frame is correct, and hence the collectionView height and tableViewCell's height are all correct.

Interestingly if I change the setup so that the UIViewController is loaded from a storyboard, and the same cells are then embedded into the storyboard's UITableView as prototype cells ... now everything works fine, and the UITableViewCell is now loading the cell's frame correctly on first pass, hence I don't need to call [tableView reloadData] to get it to size each cell correctly.

I'm not sure why it's behaving differently. Ideally I would use the nib solution, since I want to reuse the same cells in different places, and there's no way to reuse a UITableViewCell from one storyboard into another.

Any guesses or ideas as to why the UITableViewCell isn't sized properly in cellForRowAtIndexPath when loaded from a nib file vs when loaded from a storyboard?

1

1 Answers

0
votes

I think in your case, when you use nib files, they render before your viewcontroller's view size change according to your simulator. And because of viewcontroller's view size is not true, tableview size in the view is false, too. While you reload the table or scroll the tableview, view sizes getting your simulator device size, true sizes. Then cells fit the table.You can see that, when you debug; the size of view is equal to device size in your storyboard.