In a UITableViewCell subclass I override layoutSubviews
because I need to calculate the frames of a couple of subviews. The base for all of my calculations is the width of the content view. This is how the beginning of my implementation of layoutSubviews
looks like:
- (void) layoutSubviews
{
[super layoutSubviews]; // invoke this to set up the content view's bounds
CGFloat contentViewWidth = self.contentView.bounds.size.width;
[...] // calculate and assign frames to subviews
}
I have started testing this in a grouped table view in the iPhone simulator. The cell is supposed to consist of only what's inside the content view, i.e. I have turned off all the surrounding fancy stuff. Specifically, I have disabled the accessory view by setting the accessory type to UITableViewCellAccessoryNone
. Due to this, the accessory view is hidden, although its bounds/frame properties still report a size of 20/20.
Based on all this, and the following simple illustration, I would expect the content view width in the above code snippet to be reported as 300.
<----------- screen width = 320 ----------->
+------------------------------------------+
| |
| <--- content view width = 300 ---> |
| +--------------------------------+ |
|<-->| table view cell |<-->|
| 10 +--------------------------------+ 10 |
| |
| [...] |
But it's not, the content view width that is actually reported is 270!
A bit of research shows that the 30 missing points are comprised of a) 20 width of the accessory view, and b) 10 spacing between content view and accessory view. I tried setting the accessory view size to 0/0, the effect was that the content view width is now reported as 290. Slightly better, but still 10 points off. I also tried setting the accessoryView
property to nil
, but the view is simply re-created by [super layoutSubviews]
.
Finally, the question: Is there a way to really disable the accessory view so that it is not included in the calculation of the content view width? Alternatively, would it be safe to skip invoking [super layoutSubviews]
and simply calculate the width myself?
After revisiting this question I find that I can no longer reproduce the behaviour described in the question. The behaviour is now as expected:
UITableViewCellAccessoryNone
accessoryView
property returns nil
I am now testing this in Xcode 4.2 on the iPhone 5.1 simulator. When I asked the question I believe I still had the iPhone 4.3 simulator running. I must now assume that the problem either exists only in the 4.3 simulator, or was due to some Xcode misconfiguration on my part, or was entirely imaginary in the first place.
For the record: My workaround was to apply a new width to the content view in layoutSubviews
:
- (void) layoutSubviews
{
[super layoutSubviews];
CGRect contentViewFrame = self.contentView.frame;
contentViewFrame.size.width = 302;
self.contentView.frame = contentViewFrame;
[...]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With