点击UITableViewCell里的CollectionViewCell进行跳转

来源:互联网 发布:服装店记账软件 编辑:程序博客网 时间:2024/05/17 22:02

img
结构如图所示,目的是要通过点击嵌在UITableViewCell里的UICollectionViewCell来进行跳转。

但是UITableViewCell不是个controller,所以UICollectionViewCell点不出来页面跳转的方法。

我的解决办法是获取UITableViewCell所在的controller,然后通过该controller进行页面跳转


1、获取所在的最上层的controller

// MARK: - 查找所在的ViewController    func responderViewController() -> UIViewController? {        for view in sequence(first: self.superview, next: {$0?.superview}) {            if let responder = view?.next {                if responder.isKind(of: UIViewController.self) {                    return responder as? UIViewController                }            }        }        return nil    }

2、点击跳转处理方法

func Tap(_ recognizer:UITapGestureRecognizer) {        // MARK: - secondVC是目标页面        let secondVC = ImagePreviewViewController(images: pictures, index: index)        // MARK: - firstVC是所在页面        let firstVC = self.responderViewController()        firstVC?.present(secondVC, animated: true, completion: nil)}

3、在这个UICollectionViewCell所在的UICollectionView代理方法里添加点击,我用的是tap手势点击监听

let tapSingle = UITapGestureRecognizer(target: self, action: #selector(Tap(_:)))        tapSingle.numberOfTapsRequired = 1        tapSingle.numberOfTouchesRequired = 1        cell.imageView.addGestureRecognizer(tapSingle)

如有问题,欢迎斧正~

0 0
原创粉丝点击