I know that when UIKit renders a cell, it uses tableView:heightForRowAtIndexPath: to calculate the height. My question is, how and when does that get set on the actual UITableViewCell. I want to build dynamic cells and will need to calculate the placement of text within the cell. I believe I can just use self.bounds and self.frame – I was just curious about at what point those are set – even with the use of dequeueReusableCellWithIdentifier.
Thanks.
After you return the cell from
tableView:cellForRowAtIndexPath:, the tableview sets a number of properties, including the bounds of the cell, then callstableView:willDisplayCell:forRowAtIndexPath:. The only modifications after that call are setting alpha/frame, and even then only when animating the row.Given that, if you’re using a stock
UITableViewCell, you can layout your text intableView:willDisplayCell:forRowAtIndexPath:and be assured that the cell already has the correct width/height. That said, if you’re doing anything more than just the basics, you may want to consider subclassingUITableViewCell. In your subclass, you can lay out your fields inlayoutSubviewsand be assured that your cell will always be laid out correctly.