4

My app subclasses a UITableViewCell and implements layoutSubviews to modify the cell's contentView's width, like so:

- (void)layoutSubviews {
    [super layoutSubviews];
    // position subviews...
    CGRect frame = [[self contentView] frame];
    frame.size.width -= 20;
    [[self contentView] setFrame:frame]; 
}

When running this code with the iOS 8 simulator and Xcode 6 GM seed, this triggers an infinite loop. However, when running on a real iPhone or iPad running iOS 8 GM seed, it does not loop, as in previous versions of iOS.

I first thought the difference was a compiler optimization, but the simulator loops in both debug configuration and release configuration.

Questions

  • Why the difference between the iOS 8 GM and the simulator?
  • Is this a critical bug fix? I'm very reluctant to release an app that exhibits a potentially serious hanging bug, even if I can't reproduce it on device.
  • What in your opinion is the best way to refactor this to eliminate the looping without causing regression on iOS 7 and 6?
Steveo
  • 2,238
  • 1
  • 21
  • 34
  • This code looks bad, imagine something happening on screen that causes all views to re-layout their subviews: your content views can be easily srank to zero width. Not saying that this is what's happening on simulator, but I believe if you have such good attitude in regards to resolving possible bugs you might as well fix this issue. I'm yet to download the GM seed, then I shall test your code to see if the issue is easy to be reproduced, meanwhile it might be a good idea for you to pick one of the cells and post the backtrace of recursive (not the first) method calls. – A-Live Sep 11 '14 at 14:34

1 Answers1

4

This happened to me on one of the previous iOS 8 betas. Occurred on both device and simulator. After some debugging I found out that UITableViewCell probably uses autolayout internally on iOS8. Moreover, any changes to contentView.frame triggered layoutSubviews (which might be a reason of your infinite loop as well).

As a workaround, I added a subview to the cell's contentView and modified its frame instead. Then I used this view like I would use the contentView (as a superview of all the custom cell elements).

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
  • Could you elaborate a little more on how to do this? I am having the same problem. I want to make sure detailView is adjusted to the correct width in landscape mode. Thanks! – Ra1nWarden Nov 08 '14 at 01:29
  • I'm not sure what is your exact problem. I created a gist showing the idea in code: https://gist.github.com/mciuba/5b9bb9842eb141bea01e. If this doesn't help, maybe ask a new question and provide more details there. – Michał Ciuba Nov 14 '14 at 10:23
  • You are awesome, this would have taken me literally days. I was manually calculating the height of the cell and changing the contentView frame in layoutSubviews. Turning off autolayout on nib file fixed the issue. – aryaxt Aug 14 '15 at 20:25