views:

3386

answers:

3

On an iPhone, how do you figure out the width of a table view cell's content view when it is showing a certain accessory view (disclosure indicator, etc)?

I need this in order to calculate the correct cell height for cells that contain wrapping, variable-length text. But when the table view delegate is asked for a cell height, it doesn't actually have the actual cell instance, so it can't just query the content view bounds directly.

I can easily hard-code a 20-pixel margin on the right which appears to be accurate for a plain style table view in portrait orientation with a disclosure indicator, but would prefer to do it the Right Way so that it keeps working if Apple decides to tweak the margin.

(This is related to this question.)

A: 

I would have a subclass of UITableViewCell that holds all its subelements. You can cange the frame of certain elements when the cell enters and exists editing mode. There is a good example of this in Apple's Table View Programming guide under the section on creating a custom table view cell.

Brad Smith
A: 

I believe the UITableViewCell's contentView property is the view that contains your labels etc., so the width of that should be your available size to use.

Ed Marty
The problem is that the table view delegate needs to know the heights of the cells that are not visible, and thus have no actual cell instance from which to get the contentView. I suppose I can just get _a_ visible cell's contentView and assume all other cells will have the same contentView width.
Daniel Dickison
You could try just removing the height method from the delegate, then later set the height when setting up the cell, using the method in UITableView
Ed Marty
@Ed -- that won't work because the table view delegate must tell the table view the height of all cells even before their are set up so that the scroll view gets the correct dimensions.
Daniel Dickison
Looking at the contentView doesn't seem to do the trick anyway. I have a cell with a disclosure indicator, but the contentView's width is still 320. The accessory view seems to be simply overlaid over the content view. I'm looking for a clean solution as well, but for now I suppose I'll just hardcode a 20 pixel margin.
Mirko Froehlich
+1  A: 

Personally, I would just hard code the values -- simpler and things will break in a predictable way.

But were I to do this programmatically, I would create a UITableViewCell, set up the editing properties / accessory views you need to measure, and then ask it how big its contentView is.

Of course I would probably heavily cache this -- doing allocations when asking UITableView asks you for height information sounds to me like it would be slow (check with a profiler first though, as always).

Colin Barrett
Yeah, I did end up hardcoding this (it's 20 pixels).I think I tried creating a dummy cell like you described when I was first attempting this, and, if I'm remembering correctly, it didn't work. I think the layout only gets set when a cell is actually displayed in a table view.
Daniel Dickison
Oh well! Glad to hear you found a solution though.
Colin Barrett