Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong contentView width in custom UITableViewCell?

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?

like image 207
herzbube Avatar asked Nov 05 '12 00:11

herzbube


1 Answers

After revisiting this question I find that I can no longer reproduce the behaviour described in the question. The behaviour is now as expected:

  • The default accessory style of the table view cell is UITableViewCellAccessoryNone
  • The accessoryView property returns nil
  • The content view width is reported as 302

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;

  [...]
}
like image 73
herzbube Avatar answered Nov 01 '22 18:11

herzbube