1

tableViewCell contains viewController which height is calculated after tableView finish laying out views. So I need to trigger tableView to request again height of rows by calling heightForRowAtIndexPath method.

I have tried setNeedsLayout, it does not work, system will not call heightForRowAtIndexPath method.

reloadData or reloadRowsAtIndexPaths is not good choice, because it will reload all the cells, and all the viewControllers built into the cells, and again when the viewController will finished layout process and viewDidLayoutSubviews is called, reloadData will called again, so it is an infinite loop.

I was testing tableView.beginUpdates() tableView.endUpdates() empty update operation. Sometimes it triggers heightForRowAtIndexPath, sometimes not.

János
  • 32,867
  • 38
  • 193
  • 353

2 Answers2

0

heightForRowAtIndexPath: is part of the protocol UITableViewDelegate and gets called when you perform: [tableView reloadData];

setNeedsLayout is a method of UIView and causes the view to redraw itself, but it won't trigger a tableView to update itself.

nburk
  • 22,409
  • 18
  • 87
  • 132
  • if there are only certain cells that you want to update, you can use `(void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation`. but you'll have to call either of those if you want the height of your row to change... :/ – nburk Oct 15 '14 at 11:18
  • Can you show some code, maybe that'll help. If you run in an infinite loop by calling `reloadData` it sounds like there might be an issue with your code somewhere, maybe I can help out :) – nburk Oct 15 '14 at 11:23
0

You can test the setNeedsDisplay in the UIView, this will redraw the view .But the setNeedsLayout is use to layout the subviews (position and size ). If redrawing, the size will re get from the function of delegate.

see setNeedsLayout and setNeedsDisplay

Community
  • 1
  • 1
Feng Lin
  • 670
  • 4
  • 8