2017-10-06 109 views
0

我需要能夠爲我的UICollectionView在Peek和Pop的提交函數中獲得indexPath.rowindexPath in viewControllerToCommit函數

func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { 

    } 

我需要這樣做,因爲在UICollectionView每一行打開一個不同的視圖控制器,需要通過對不同行的信息。有任何想法嗎?

回答

0

推薦的方法是要呈現哪個應該已經充滿了你UICollectionView的好項目viewControllerToCommit對象:

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { 
    guard let indexPath = collectionView.indexPathForItem(at: location), let cell = collectionView.cellForRow(at: indexPath) as? YOUR_CELL_CLASS else { 
     return nil 
    } 

    previewingContext.sourceRect = cell.frame 

    let viewController = YOUR_VIEW_CONTROLLER_CLASS() 
    viewController.yourObject = cell.yourObject 
    return viewController 
} 

func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { 
    navigationController?.pushViewController(viewControllerToCommit, animated: true) 
}