Swift 4 - 从 UICollectionView Cell 推送一个 ViewController

0 ios uicollectionview uicollectionviewcell swift

我被困在我的项目中,需要一些帮助。我想UICollectionViewControllerUICollectionViewCell 包括我的UINavigationBar. 我知道您实际上无法从 Cell 中呈现 ViewController?这种情况我该怎么办?我的问题是我的 NavigationBar 不会出现。

这是我的细胞:

import UIKit
import Firebase

class UserProfileHeader: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

lazy var followersLabelButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("followers", for: .normal)
    button.setTitleColor(UIColor.lightGray, for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
    button.addTarget(self, action: #selector(followers), for: .touchUpInside)
    return button
}()

@objc fileprivate func followers() {

    let newController = NewController(collectionViewLayout: UICollectionViewFlowLayout())

    self.window?.rootViewController?.present(newController, animated: true, completion: nil)


    }

   }
Run Code Online (Sandbox Code Playgroud)

和 CollectionViewController:

import UIKit
import MapKit
import Firebase

class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate {

    var userId: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.backgroundColor = UIColor.white


        collectionView?.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerId")

    }


    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath) as! UserProfileHeader

        header.user = self.user

        return header
    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {


        return CGSize(width: view.frame.width, height: 200)


    }


}
Run Code Online (Sandbox Code Playgroud)

Sh_*_*han 5

1- 细胞内部

weak var delegate:UserProfileController?
Run Code Online (Sandbox Code Playgroud)

2- 内部 cellForItemAt

cell.delegate = self
Run Code Online (Sandbox Code Playgroud)

3-在单元格内使用它

delegate?.present(newController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我想你的意思是

delegate?.navigationController?.pushViewController(newController, animated: true)  
Run Code Online (Sandbox Code Playgroud)