1

I use Any-Any Size in Storyboard and define the following constraints for UITableViewCell,

  1. Leading Space To SuperView -16.00px
  2. Trailing Space To SuperView -16.00px
  3. Top Space to SuperView 0.00px
  4. Bottom Space to SuperView 0.00px

When I print the frame of the UITableViewCell For all sizes of iphone,i get the frame size

BookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BookTableViewCellID"];
if (cell==nil) {
   cell = [[BookTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"BookTableViewCellID"];
} 
NSLog(@"TextView Cell Frame %@ ",NSStringFromCGRect( cell.contentView.frame));

And the output, textView cell frame {{0, 0}, {600, 44}}. Here width is 600 which is same as the width for Any-Any Size.

But for iPhone5, i expect the width here 320. How Can i get the auto-resized width of the UITableViewCell.

Gandalf
  • 2,399
  • 2
  • 15
  • 19
Rabindra Nath Nandi
  • 1,433
  • 1
  • 15
  • 28

1 Answers1

1

I think I just encountered a similar problem, and I think I've solved it.

My issue stemmed from converting a Storyboard originally set up in XCode 4.7 for iOS 6 to xCode 7 for iOS 9 (which, I gather is the same as xCode 6 and iOS 8). Everything was working great with the old iPhone specific size in Simulated Metrics, but I encountered a similar problem when converting everything to the 600x600 size for the new Autolayout.

I had all my subviews resizing and spacing properly, but a frame I was adding to my custom UITableViewCells was drawing incorrectly: the left margin was correct, as was the height, but the frame was being drawn off the screen to the right. The rub was that all the subviews within the cell (a UILabel and a UISwitch) were drawing in the correct locations.

This led me to realize that, like the OP, I was adding the sublayer that drew my frame in cellForRowAtIndexPath. Apparently, with the 600x600 sized templates in the new Autolayout, a UITableViewCell's width is not yet set after dequeReusableCellWithIdentifier, but later in the setup. This made me realize that we probably shouldn't be doing anything involving a cell's dimensions in cellForRowAtIndexPath, but just setting properties and whatnot, unless I'm completely not understanding this.

The solution: implement a subclass of UITableViewCell and do all your drawing in drawRect. Your constraints should all be set correctly by then.

paulmrest
  • 414
  • 4
  • 14