我需要能够在Peek和Pop的提交函数中获取indexPath.rowUICollectionView

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

    }

我需要这样做,因为UICollectionView中的每一行都会打开一个不同的视图控制器,并且需要为不同的行传递信息。有任何想法吗?

最佳答案

推荐的方法是显示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)
}

关于ios - viewControllerToCommit函数中的IndexPath,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46604559/

10-12 04:22