When I used the code you provide to test, it got into a loop. It would set the kRowHeight to h, then it would try to call cellForRowAtIndexPath
, and then repeat. Apparently, cellForRowAtIndexPath
is run BEFORE cellForRowAtIndexPath
. It is discussed in the top answer to this question Iphone - when to calculate heightForRowAtIndexPath
for a tableView
when each cell height is dynamic?
One way to determine the correct height is to check elsewhere about the reason to change the size. In that shared answer, they mention checking sizeWithFont if you have different fonts in the boxes.
In your case, you might be able to extract the part of your cellForRowAtIndexPath
that determines whether the label is there or not, and then call that in your cellForRowAtIndexPath
as well as here in heightForRowAtIndexPath
. You extract it so you don't duplicate the code, and you just call it in two different places.
You may also have to run [self.tableView reloadData]
. You maybe even have to call [self.view setNeedsLayout]
(but this one I think is if you are updating it later, probably is not necessary in viewWillAppear).