首页 > 解决方案 > 将 ViewController 添加到 UICollectionviewcell

问题描述

我想在 UICollectionView 单元格中嵌入一个 ViewController,我试图在互联网上搜索,但我没有找到任何适用于我的案例的东西。

到目前为止,我已经这样做了,

这是视图控制器:

class ExploreViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .systemGreen
}
}

这是 collectionViewCell:

class ExploreCollectionViewCell: UICollectionViewCell {

let exploreViewController = ExploreViewController()

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview(exploreViewController.view)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

基本上我在单元格内实例化一个 ViewController 并显示它的视图;

我不认为这实际上是最好的事情,但老实说,我不知道如何以不同的方式实现它。

最好的办法是将 viewController 实际嵌入到单元格中,你知道我该怎么做吗?

标签: swiftuiviewcontrolleruicollectionviewuicollectionviewcell

解决方案


推荐阅读