0

My app was working fine without 3Dtouch implementation; But with the 3Dtouch added the app continues to work great and rotates normally until 3D touch is used (peek or pop);I had a tableViewCell handled peek/pop and the preview delegate.The presentation would be done twice and trigger this in the console:@"Warning: Attempt to present on which is already presenting (null)

1 Answers1

0

I have experienced the same problem. The cause of it is that you are registering the table view cell in cellForRowAtIndexPath.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // myTableViewCell is created or reused here
    ...
    registerForPreviewingWithDelegate(self, sourceView: myTableViewCell)
    ...

}

This causes the cell to be registered multiple times when it is reused by the table view. But registering the same view multiple times with registerForPreviewingWithDelegate is not allowed and leads to the warning and rotation problems you describe.

See the Apple documentation

You can designate more than one source view for a single registered view controller, but you cannot designate a single view as a source view more than once.

The solution is simple. Check if the view is already registered before you register it a second time and unregister if necessary.

private var previewingContexts = [MyTableViewCellClass: UIViewControllerPreviewing]()

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // myTableViewCell is created or reused here
    ...
    if let previewingContext = previewingContexts[myTableViewCell] {
        unregisterForPreviewingWithContext(previewingContext)
    }

    let previewingContext = registerForPreviewingWithDelegate(self, sourceView: myTableViewCell)
    previewingContexts[myTableViewCell] = previewingContext
    ...

}

You can also use the method described here: http://krakendev.io/peek-pop/ This uses the location parameter which is passed to the delegate to find out which cell was tapped. In this way you just have to register the whole table view and not every single cell. This might be an ever better solution. In my case this was not possible because I have nested collection views inside tableview cells, so it is way more complex.

Darko
  • 9,655
  • 9
  • 36
  • 48