Swift 4 - 从 UICollectionView Cell 推送一个 ViewController [英] Swift 4 - Push a ViewController from a UICollectionView Cell
问题描述
我被困在我的项目中,需要一些帮助.我想从 UICollectionViewCell
包括我的 UINavigationBar
推送/呈现一个 UICollectionViewController
.我知道您实际上无法从 Cell 中呈现 ViewController?这种情况我该怎么办?我的问题是我的 NavigationBar 不会出现.
I'm stuck in my project and is in need for some help. I want to push/present a UICollectionViewController
from a UICollectionViewCell
including my UINavigationBar
. I have understand that you can not actually present a ViewController from a Cell? How do I supposed to do in this case? My problem is that my NavigationBar won´t show up.
这是我的手机:
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)
}
}
和 CollectionViewController:
And the 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)
}
}
推荐答案
1- 单元格内部
weak var delegate:UserProfileController?
2- 内部 cellForItemAt
cell.delegate = self
3- 在单元格内使用它
3- Use this inside the cell
delegate?.present(newController, animated: true, completion: nil)
顺便说一句,我想你的意思
BTW i think you mean
delegate?.navigationController?.pushViewController(newController, animated: true)
这篇关于Swift 4 - 从 UICollectionView Cell 推送一个 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!