views:

55

answers:

1

Hi,

I have a UITableViewCell with two subviews, a UILabel on the left, and a random input control on the right. The random input control on the right can vary in size, as can the length of the text, but since I can set the word wrap of the text on the left, I need to be able to adjust the size of the UILabel based on the width of the random input control. To complicate matters, the app needs to work in both portrait and landscape modes, which give the table cells different widths.

This wouldn't be difficult if I could read the width of the table cells and set the widths of its subviews appropriately, but at creation time the width of the cell is 0.

Any ideas?

+2  A: 

Nothing easier than that: every UITableViewCell is also a UIView, which has a method designed for just that: layoutSubviews, which is called whenever the view (here: cell) needs a re-layout. This is where you lay out the content.

Max Seelemann
That also means you have to subclass UITableCell if its not already so (that's also the best solution to add your label and the other control to the cell)
VdesmedT
Ah, I was wondering why tableView:cellForRowAtIndexPath: wasn't getting called when the device orientation changed! Give me a few to try your suggestion out.
JoBu1324
If you call some method to reload the table view when orientation changed, then tableView:cellForRowAtIndexPath will be called.
Toro
layoutSubviews was definitely what I wanted. I knew it was possible to reload the table upon orientation changes, but that's more expensive, and it's not the way the UITableView was designed.
JoBu1324
Whoops, I forgot to mention - I found out I could get the width of the UITableView using the bounds property, which is where the real magic happens, so the question marked answered is actually only 1/2 the answer.
JoBu1324