1

iOS 9, Xcode 7, Swift 2.2

I have a Storyboard containing one UIViewController, which in turn contains a single UITableView.

The UITableView is pinned to the edges of the UIViewController's view using Constraints.

Design Requirement

When the app is used on an iPad in full-screen mode (i.e. Regular for both horizontal and vertical size classes), the margins of the tableview need to change, depending on the available width of the tableview. Effectively, when in portrait, the margin should be 100 points, but 200 points in a landscape environment. The tableview needs to occupy the entire view area for usability (e.g. scrolling, tapping gestures anywhere within the view.)

Current Approach

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
    let margins: UIEdgeInsets
    if (tableView.bounds.size.width < kWidthThreshold) {
        margins = UIEdgeInsetsMake(0, 100, 0, 100)
    } else {
        margins = UIEdgeInsetsMake(0, 200, 0, 200)
    }
    cell.separatorInset = margins
    cell.layoutMargins = margins
}

Problem

Following app launch, the view is displayed correctly. Device rotation results in cells honouring the correct margins for the device orientation (or, more accurately, change in view width).

But, if I then scroll the tableview, newly appearing cells do not honour the margins. Their layout is broken, in an apparently random manner.

Questions

Should I be doing anything else to handle the change in margins?

In this scenario, when should I be calling reloadData on the tableview?

Womble
  • 4,607
  • 2
  • 31
  • 45

0 Answers0