【问题标题】:Choose cell in collection view to new controller选择集合视图中的单元格到新控制器
【发布时间】:2019-02-16 18:57:31
【问题描述】:

对编码和以编程方式制作项目非常陌生。我的收藏视图已全部设置,但我不知道如何告诉特定单元格导航到新的收藏视图或详细信息视图。非常困惑和沮丧。任何人都可以帮助我或至少指出我正确的方向。请把它弄小点哈哈。

这是我的主视图控制器

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 文件?然后告诉那个单元格到那个快速文件来显示内容??谢谢你的时间。

【问题讨论】:

    标签: ios swift cell collectionview


    【解决方案1】:

    不,您不需要为每个单元格创建自己的viewController.swift 文件。您只需要创建一个详细页面屏幕(一个 DetailViewController.swift),并且需要使用在详细视图控制器上声明的变量传递所选单元格的详细信息。

    就像我在演示项目中使用您的代码完成 HERE 一样,结果将是:

    我所做的是在情节提要中添加了 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 = indexPath.row 传递数据(我正在传递选定的索引),因为selectedIndexDetailViewController 的属性,因为我已经在DetailViewController.swift 中声明了它

    var selectedIndex = 0
    

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

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

    【讨论】:

    • omg 终于哈哈,非常感谢你……它奏效了,我花了一点时间才得到它,但它奏效了……你太棒了。我从昨天早上 7 点到午夜一直在做这件事。感激不尽
    • 所以现在我已经全部工作了,如果我错了,请纠正我......我希望 detailViewController 成为另一个集合视图......它将基本上像 HomeController 一样设置,但有不同的选项和细胞。我会将 detailViewController.swift 文件设置为与 HomeController.swift/storyboard 相同吗...或者这可能吗?
    • 不,您不能将其设置为相同,因为 HomeController 继承自 UICollectionViewController 并且 detailViewController 继承自 UIViewController 但您可以创建新的 detailViewController 继承自 @ 987654337@和你的HomeController一样
    • 嗯。好的。有道理......我可能会重新考虑我的详细视图。再次感谢您。
    • 很高兴为您提供帮助.. :)
    【解决方案2】:

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

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

    【讨论】: