Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell's contentView's width with a given accessory type

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.)

like image 889
Daniel Dickison Avatar asked May 23 '09 18:05

Daniel Dickison


1 Answers

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).

like image 74
Colin Barrett Avatar answered Oct 19 '22 15:10

Colin Barrett