viewControllerToCommit函数中的IndexPath

时间:2017-10-06 11:19:16

标签: ios swift uicollectionview 3dtouch

我需要能够在Peek和Pop的提交函数中获取indexPath.row的{​​{1}}。

UICollectionView

我需要这样做,因为func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) { } 中的每一行都会打开一个不同的视图控制器,需要传递不同行的信息。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

推荐的方法是展示viewControllerToCommit对象,该对象应该已经填充了UICollectionView的好项:

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)
}