1
votes

This seems like a very basic operation which has been made overly complex.

I have a uitableview with cells which i am trying to resize using heightForRowAtIndexPath. I need the cells to resize based on the text they contain and so I am using attributed string boundingRectWithSize, passing in the width of the table and the height of the window and using the correct font from the dequeued cell and UsesLineFragmentOrigin.

For many cases this cuts off the last line of text and I believe it is due to an offset between the cell's content view and the tableview edges, such that when I give boundingRectWithSize the width of the table it miscaculates the height.

I can even see this offest when I turn on view debugging "show view frames." There are equal offsets on the left and right sides of the content view.

However, I cant figure out how to access this offset in the method heightForRowAtIndexPath. From view debugging, I know that the difference between the width of the table and the contentview is 40 but I dont want to hardcode that value. My best guess at where this is located is a property of UITableView called "separatorInset" which is an edgeInset which has "left" "right" "up" and "down" dimensions. However, when I inspect that property, all dimensions are zero except for "left" which is 20.

Since I know the difference is 40, if I pass the width minus "left"*2 into boundingRectWithSize, the size is correct for all my test strings.

However, this bothers me since what I would expect to pass is width - "left" - "right" and just using left*2 more or less amounts to hard coding 40.

Am I missing the real property which would tell me what the left and right offsets are or is "separatorInsets" bugged or do "left" "right" "up" "down" signify something other than the obvious? Thanks.

1

1 Answers

0
votes

I feel foolish but the term I was looking for is "margin." I was looking for something called an offset but in generic terms what I was thinking of is a margin.

I might have considered this initially but then when looking at the documentation for UITableView I failed to see any property with that word in it.

However, UIView does of course have a property called "layoutMargins" and that is what I could not find.

After circling around several times considering whether the tableview or the tableviewcell or even the contentview of the cell might be controlling this I believe it was easy to get confused so for anyone wondering, this sort of thing is controlled by the property "layoutMargins" on the UITableView.

However, I was still a bit confused because UITableViewCell has a property (also from UIView) called preservesSuperviewLayoutMargins which is supposed to default to NO in which case, the layout of the cell should override that of the tableview.

It seems that for UITableViewCell, this defaults to YES. Something to keep in mind i guess.