1

I have a project with a Universal storyboard (screen size: 600 x 600) containing a class that overrides UITableViewCell:

class MyTableCell: UITableViewCell 
{
    ...
}

Rather than utilising a prototype cell, the UITableView that displays this cell registers MyTableCell as a class:

myTableView.registerClass( MyTableCell.self, forCellReuseIdentifier: "myCell" )

MyTableCell then overrides layoutSubviews:

override func layoutSubviews ()
{
    super.layoutSubviews()
    let width = CGRectGetWidth( frame )
}

Great! However, the width that is returned is the universal 600, and not the device-specific 320 that I need.

What am I doing wrong please? Thank you.

Joseph Beuys' Mum
  • 2,395
  • 2
  • 24
  • 50

1 Answers1

2

layoutSubviews() will get called at different times in the cell's lifecycle, and can have different values as it gets moved around. When you first get it off of the storyboard it will have the width you see in the storyboard. After it gets inserted in the table view it will have the same width as the table view.

Normally this isn't a problem. It will get called with the wrong size once and then again with the correct size. But it may seem confusing in the debugger.

If you never see it change to the 320 you expect, that probably means that your table view isn't configured properly for auto layout in the storyboard, and is actually drawing at 600 pixels wide at runtime.

Put a breakpoint on that method, and try entering this into the debug console to see what the table view size is. This should return lots of information about the table view, including its size.

po superview
Dave Batton
  • 8,795
  • 1
  • 46
  • 50