Wrong content width in custom UITableViewCell?

In a subclass of UITableViewCell, I override layoutSubviews because I need to calculate frames from multiple subzones. The basis for all my calculations is the width of the content representation. Here's what the beginning of my layoutSubviews implementation looks like:

 - (void) layoutSubviews { [super layoutSubviews]; // invoke this to set up the content view bounds CGFloat contentViewWidth = self.contentView.bounds.size.width; [...] // calculate and assign frames to subviews } 

I started testing this in a grouped table format in an iPhone simulator. It is assumed that the cell consists only of what is inside the content view, i.e. I turned off all the bizarre stuff around. In particular, I turned off accessory viewing by setting the accessory type to UITableViewCellAccessoryNone . Because of this, the appearance of the accessories is hidden, although its border / frame properties still report a 20/20 size.

Based on all this and the following simple illustration, I expected the width of the content representation in the above code snippet to be reported as 300.

 <----------- screen width = 320 -----------> +------------------------------------------+ | | | <--- content view width = 300 ---> | | +--------------------------------+ | |<-->| table view cell |<-->| | 10 +--------------------------------+ 10 | | | | [...] | 

But this is not so, the actual information on the width of the content presentation is 270!

A little research shows that 30 missing points consist of a) 20 widths of the auxiliary view and b) 10 intervals between the presentation of the content and the accessory. I tried to set the size of the accessory view to 0/0, the effect was that now the width of the content view is 290. A little better, but still 10 points. I also tried setting the accessoryView property to nil , but the view is simply recreated [super layoutSubviews] .

Finally, the question is: is there a way to really disable the appearance of the accessory so that it is not included in the calculation of the width of the content view? Alternatively, would it be safe to skip the [super layoutSubviews] call and just calculate the width yourself?

+6
source share
1 answer

After revising this question, I found that I can no longer reproduce the behavior described in the question. The behavior is now as expected:

  • Default accessory style for table view cell: UITableViewCellAccessoryNone
  • The accessoryView property returns nil
  • The width of the content view is displayed as 302

Now I am testing this in Xcode 4.2 on the iPhone 5.1 simulator. When I asked a question, I believe that I still had an iPhone 4.3 simulator. Now I have to assume that the problem either exists only in simulator 4.3, or was caused by an incorrect Xcode configuration on my part, or was completely imaginary in the first place.

For the record: My workaround was to apply the 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; [...] } 
+7
source

All Articles