Ios 在“集合”视图中选择单元格以创建新控制器

Ios 在“集合”视图中选择单元格以创建新控制器,ios,swift,cell,collectionview,Ios,Swift,Cell,Collectionview,对编程和以编程方式创建项目非常陌生。我已设置了所有集合视图,但我不知道如何告诉特定单元格导航到新集合视图或详细视图。非常困惑和沮丧。谁能帮我,或者至少给我指出正确的方向。请安静下来,哈哈 这是我的主视图控制器 import UIKit class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { var Legends: [Legend] = { var s

对编程和以编程方式创建项目非常陌生。我已设置了所有集合视图,但我不知道如何告诉特定单元格导航到新集合视图或详细视图。非常困惑和沮丧。谁能帮我,或者至少给我指出正确的方向。请安静下来,哈哈

这是我的主视图控制器

import UIKit



class HomeController: UICollectionViewController, 
UICollectionViewDelegateFlowLayout {


    var Legends: [Legend] = {
    var select1 = Legend()
    select1.thumbnailImageName = "select1thumbnail"
    var select2 = Legend()
    select2.thumbnailImageName = "select2humbnail"

    return[select1, select2]        
}()

    override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = "Choose Selection"
    collectionView.backgroundView = UIImageView(image: UIImage(named: "backgroundlogo"))
    collectionView?.register(VideoCell.self, forCellWithReuseIdentifier: "cellId")
    collectionView.dataSource = self
    collectionView.delegate = self



    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return Legends.count

}
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! VideoCell

    cell.legend = Legends[indexPath.item]       
    return cell

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width:view.frame.height, height: 150)
    }

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0

}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let select2VC = UIViewController()
    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.pushViewController(select2VC, animated: true)

 print("selcected")


}

}
这是我收藏视图中的swift文件

import UIKit



class VideoCell: UICollectionViewCell {

var legend: Legend? {
    didSet {
        thumbnailImageView.image = UIImage(named: (legend?.thumbnailImageName)!)

    }
}

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

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

let thumbnailImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.backgroundColor = UIColor.darkGray
    imageView.image = UIImage(named:"bgcolor")
    imageView.contentMode = .scaleAspectFit
    imageView.clipsToBounds = true
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

func setupViews() {
    addSubview(thumbnailImageView)

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]-16-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": thumbnailImageView]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-1-[v0]-0-|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": thumbnailImageView]))

}



}
在第三个文件中,我只有这段代码

import UIKit

    class Legend: NSObject {

    var thumbnailImageName: String?

}

主视图控制器的底部代码会打印出一个“选定的”,但它会为每个单元格打印它。。。我假设每个单元格都需要自己的viewController.swift文件?然后告诉该单元格到swift文件以显示内容??谢谢您的时间。

不,您不需要为每个单元格创建自己的
viewController.swift
文件。您只需要创建一个详细页面屏幕(一个DetailViewController.swift),并且需要通过在detail view controller上声明的变量传递所选单元格的详细信息

就像我在演示项目中使用您的代码所做的那样,结果将是:

我所做的是在故事板中添加了detailViewController,还添加了类文件。而
didSelectItemAt
将如下所示:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
    controller.selectedIndex = indexPath.row //pass selected cell index to next view.
    self.navigationController?.pushViewController(controller, animated: true)

}
在这里,您可以使用
controller.selectedIndex=indexath.row
(在这里我传递的是所选索引),因为
selectedIndex
DetailViewController
的属性,因为我在
DetailViewController.swift
中声明了它

var selectedIndex = 0
同样,您也可以传递与所选索引相关的其他数据


有关更多信息,请参阅演示项目。

否您不需要为每个单元格创建自己的
viewController.swift
文件。您只需要创建一个详细页面屏幕(一个DetailViewController.swift),并且需要通过在detail view controller上声明的变量传递所选单元格的详细信息

就像我在演示项目中使用您的代码所做的那样,结果将是:

我所做的是在故事板中添加了detailViewController,还添加了类文件。而
didSelectItemAt
将如下所示:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
    controller.selectedIndex = indexPath.row //pass selected cell index to next view.
    self.navigationController?.pushViewController(controller, animated: true)

}
在这里,您可以使用
controller.selectedIndex=indexath.row
(在这里我传递的是所选索引),因为
selectedIndex
DetailViewController
的属性,因为我在
DetailViewController.swift
中声明了它

var selectedIndex = 0
同样,您也可以传递与所选索引相关的其他数据


有关更多信息,请参阅演示项目。

如果需要,请检查所按下的按钮。您可以检查按下的单元格类型:

if let cell = tableView.cellForRow(at: indexPath) as? VideoCell {
//check some property and go to new UIViewController or UICollectionViewController
//and transfer data 
}

如果你想检查什么是卖压。您可以检查按下的单元格类型:

if let cell = tableView.cellForRow(at: indexPath) as? VideoCell {
//check some property and go to new UIViewController or UICollectionViewController
//and transfer data 
}

天哪,最后哈哈,非常感谢你。。。它起作用了,我花了一点时间才得到它,但它起作用了。。你太棒了。我从昨天早上7点到午夜一直在做这个。我不能感谢你,所以现在我有所有的工作,纠正我,如果我错了。。。我希望detailViewController是另一个集合视图。。。它的设置基本上与HomeController相似,但具有不同的选项和单元。我是否将detailViewController.swift文件设置为与HomeController.swift/storyboard相同的文件。。。或者这是可能的?不,您不能将其设置为相同的,因为
HomeController
继承自
UICollectionViewController
,而
detailViewController
继承自
UIViewController
,但您可以创建新的
detailViewController
,它将继承自
UICollectionViewController
与您的
HomeController
Hm.正确。有道理。。。。。。我可能会重新考虑我的细节观点。再次感谢您。很高兴能帮助您……)天哪,最后哈哈,非常感谢你。。。它起作用了,我花了一点时间才得到它,但它起作用了。。你太棒了。我从昨天早上7点到午夜一直在做这个。我不能感谢你,所以现在我有所有的工作,纠正我,如果我错了。。。我希望detailViewController是另一个集合视图。。。它的设置基本上与HomeController相似,但具有不同的选项和单元。我是否将detailViewController.swift文件设置为与HomeController.swift/storyboard相同的文件。。。或者这是可能的?不,您不能将其设置为相同的,因为
HomeController
继承自
UICollectionViewController
,而
detailViewController
继承自
UIViewController
,但您可以创建新的
detailViewController
,它将继承自
UICollectionViewController
与您的
HomeController
Hm.正确。有道理。。。。。。我可能会重新考虑我的细节观点。再次感谢您。很高兴能帮助您……)