Quote:
Originally Posted by pcmofo
I am having some trouble following what your saying here....
so I set up the cell.. and do [cell setText:someText]; then how do I measure the cells height and what do I do once I have that height? I assume that I am not using heightForRowAtIndexPath. Can anyone provide a quick snip of code for me to look at?
What is layoutSubviews and how to I use it? I tried looking for it in the doc.
|
You have two problems to solve here. You have to tell the table view the height of the cell, and you need to set up the subviews of the cell to the right heights.
To set the cell, you need to implement the method heightForRowAtIndexPath as you say. That method should be in your table view controller.
You also need to adjust the size of the subviews in your cell for the text.
What I did was to add a getHeightOfText and cellHeightWithText CLASS methods to my custom cell class. They are class methods because I want to be able to use the same methods in the code for my custom cell, and in the heightForRowAtIndexPath handler in my tableViewController. The logic that determines how tall a cell should be really belongs in my custom cell class, but the system asks the view controller. Creating a class method lets my heightForRowAtIndex path method ask the cell class how tall the cell should be.
The heightForRowAtIndexPath method in the table view controller is called before a cell has been set up, so it doesn't have access to an instance of a cell object. It asks my custom cell class "How tall should a cell be to display the following text?"
I don't use the label view in a default cell. instead I added my own UILabel to the cell. I added a setText method to the cell. The setText method uses the getHeightOfText class method to figure out the height of the text, and sets the frame of my text view accordingly.
In answer to your other question, layoutSubviews is an optional method for UIView that lets a view move and size it's subviews. If you have a cell with a variety of components (a couple of labels, an image, etc.) then the layoutSubviews method is a good place to put the logic that adjusts the size and location of the different parts of your cell based on the size and placement of the different parts of the cell.
Duncan C
WareTo