title: NSInternalInconsistencyException with Autolayout
body: <p>I am trying to use a custom header view for header in my table view controller.
  I have created a class for my custom uitableviewheaderfooterview.</p>
  
  <p>What is in my custom header view? It adds a button to default header view.</p>
  
  <p>I am able to use this custom header view with few uitableviewcontrollers.. But it would seem that I am not able to use it for some controllers at all.
  First table view controller from navigation controller; using my custom view is ok.
  But if I push to next view, I will run into problems fast..</p>
  
  <p>I am not posting here my custom class, due to the reason of it's length and because this can be simulated with simpler variation as well. I use storyboards, in storyboard I have a navigation controller and tableview controller, and my custom view is used there. A cell pushes to another tableview controller. This also uses the custom class, but hangs immediately when it gets pushed with:</p>
  
  <pre><code> 2014-07-27 13:10:04.365 Hours[41029:3941001] *** Terminating app due
   to uncaught exception 'NSInternalInconsistencyException', reason:
   'Modifications to the layout engine must not be performed from a
   background thread.'
   *** First throw call stack: (  0   CoreFoundation                      0x0000000108beec35 __exceptionPreprocess + 165  1   libobjc.A.dylib   
   0x000000010ab76a1c objc_exception_throw + 45   2   CoreFoundation      
   0x0000000108beeb6d +[NSException raise:format:] + 205  3   Foundation 
   0x000000010924decc -[NSISEngine withBehaviors:performModifications:] +
   89     4   UIKit                               0x0000000109f8a2d9
   __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452   5   UIKit                               0x0000000109f8a0ec -[UIView(AdditionalLayoutSupport)
   _switchToLayoutEngine:] + 197  6   UIKit                               0x0000000109f89d88 -[UIView(AdditionalLayoutSupport)
   _initializeHostedLayoutEngine] + 404   7   UIKit                               0x0000000109f8a881 -[UIView(AdditionalLayoutSupport)
   _layoutEngineCreateIfNecessary] + 53   8   UIKit                               0x0000000109f7f53f -[UIView(UIConstraintBasedLayout)
   _withAutomaticEngineOptimizationDisabled:] + 22    9   UIKit                               0x0000000109f7ff20 -[UIView(UIConstraintBasedLayout) addConstraints:]
   + 263  10  Hours                               0x0000000108282db4 _TFC5Hours16EventTypesEditor9tableViewfS0_FTGSQCSo11UITableView_22viewForHeaderInSectionSi_GSQCSo6UIView_
   + 7012     11  Hours                               0x0000000108283e56 _TToFC5Hours16EventTypesEditor9tableViewfS0_FTGSQCSo11UITableView_22viewForHeaderInSectionSi_GSQCSo6UIView_
   + 70   12  UIKit                               0x0000000109a0bb7e -[UITableView _delegateViewForHeaderInSection:] + 45     13  UIKit                               0x0000000109a0f124 __96-[UITableView
   _sectionHeaderView:withFrame:forSection:floating:reuseViewIfPossible:willDisplay:]_block_invoke
   + 97   14  UIKit                               0x000000010998ab9e +[UIView(Animation) performWithoutAnimation:] + 65   15  UIKit                               0x0000000109a0efcf -[UITableView
   _sectionHeaderView:withFrame:forSection:floating:reuseViewIfPossible:willDisplay:]
   + 511  16  UIKit                               0x0000000109a0f8c0 -[UITableView _sectionHeaderViewWithFrame:forSection:floating:reuseViewIfPossible:willDisplay:]
   + 85   17  UIKit                               0x00000001099ef030 -[UITableView _updateVisibleHeadersAndFootersNow:] + 2582    18  UIKit                               0x00000001099f06a5 -[UITableView _updateVisibleCellsNow:isRecursive:]
   + 3714     19  UIKit                               0x0000000109a05fea -[UITableView layoutSubviews] + 213  20  UIKit                               0x0000000109992d25 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
   + 519  21  QuartzCore                          0x00000001097a9058 -[CALayer layoutSublayers] + 150     22  QuartzCore                          0x000000010979dc7e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE +
   380    23  QuartzCore                          0x000000010979daee
   _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24     24  QuartzCore                          0x000000010970c8e6
   _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242    25  QuartzCore                          0x000000010970d9f2
   _ZN2CA11Transaction6commitEv + 390     26  QuartzCore                          0x000000010970dc27 _ZN2CA11Transaction14release_threadEPv + 199     27 
   libsystem_pthread.dylib             0x000000010b45f54f
   _pthread_tsd_cleanup + 86  28  libsystem_pthread.dylib             0x000000010b45c479 _pthread_exit + 111  29  libsystem_pthread.dylib   
   0x000000010b45c3e9 pthread_exit + 30   30  Foundation                  
   0x0000000109282e51 __NSFinalizeThreadData + 0  31  Foundation         
   0x0000000109262e41 __NSThread__main__ + 1214   32 
   libsystem_pthread.dylib             0x000000010b45b899 _pthread_body +
   138    33  libsystem_pthread.dylib             0x000000010b45b72a
   _pthread_struct_init + 0   34  libsystem_pthread.dylib             0x000000010b45ffc9 thread_start + 13 ) libc++abi.dylib: terminating
   with uncaught exception of type NSException
  </code></pre>
  
  <p>Here is a variation of custom view that causes this issue:</p>
  
  <pre><code>    let customSection = 0
  
      override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -&gt; UIView! {
          switch section {
          case customSection:
              var h = UITableViewHeaderFooterView()
  //          var h = self.tableView.dequeueReusableHeaderFooterViewWithIdentifier("Header") as UITableViewHeaderFooterView // This also caused trouble
  
              h.contentView.autoresizingMask = .FlexibleWidth | .FlexibleHeight
              h.contentView.setTranslatesAutoresizingMaskIntoConstraints(true)
  
              var t = UILabel(frame: CGRectZero)
              t.text = "Hello"
  //          t.sizeToFit()
              t.setTranslatesAutoresizingMaskIntoConstraints(false)
              h.contentView.addSubview(t)
  
              h.layoutSubviews()
  
              var views = NSMutableDictionary()
              views.setValue(h.contentView, forKey: "|")
              views.setValue(t, forKey: "label")
  
              h.contentView.addConstraints([
                  NSLayoutConstraint.constraintsWithVisualFormat("H:[label]-|", options: nil, metrics: nil, views: views)
                  ])
  
              h.contentView.addConstraints([
                  NSLayoutConstraint.constraintsWithVisualFormat("V:|-[label]", options: nil, metrics: nil, views: views)
                  ])
  
  
              return h
          default:
              return super.tableView(tableView, viewForHeaderInSection: section)
          }
      }
  </code></pre>
  
  <p>Possibly.. But not sure. Not familiar with threads..
  How the magic happens? This is how.. When you tap the cell with push action..
  didselectrowatindexpath is activated and it does the following:</p>
  
  <pre><code>override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
          tableView.deselectRowAtIndexPath(indexPath, animated: true)
          let cell = tableView.cellForRowAtIndexPath(indexPath)
          cell!.selectCell()
      }
  </code></pre>
  
  <p>Now, to selectCell..</p>
  
  <pre><code>override func selectCell() {
  
          UIApplication.sharedApplication().sendAction("resignFirstResponder", to: nil, from: nil, forEvent: nil)
  
          if self.buttonSelector != nil {
              let time = dispatch_time(DISPATCH_TIME_NOW, Int64(0.0 * Double(NSEC_PER_MSEC)))
              dispatch_after(time, dispatch_get_main_queue(), {
                  NSThread.detachNewThreadSelector(self.buttonSelector, toTarget:self.buttonTarget, withObject: self)
                  })
          }
  
      }
  </code></pre>
  
  <p>selectCell is included as a empty function in uitableviewcell's extension..
  and finally, what is set as buttonSelector? Function in uitableviewcontroller doing the push.. It's here:</p>
  
  <p>func eventTypeDetails(cell: UIMenuItemCell) {
      NSLog("Details for %@", (eventTypes.objectAtIndex(cell.tag) as NSArray).objectAtIndex(1) as String)</p>
  
  <pre><code>let cell = self.tableView.dequeueReusableCellWithIdentifier("eventTypeCell") as SettingsActionCell
  cell.eventTypesButton!.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
  </code></pre>
  
  <p>}</p>
  
  <p>I am starting to see that it's my override func selectCell() that is causing this, but how to fix this..??? Simple fix propably is to change didselectrowatindexpath to do the pushing accordingly to index paths.. But I wonder if this could be fixed so I could maintain my original way of doing things? After all, I've got many custom classes based on uitableviewcell to help making form like views and stuff.. All with autolayout and all using this same approach (some of them have buttons or other kind of actions)</p>