gpt4 book ai didi

ios - 选择 Collection View 中的单元格到新 Controller

转载 作者:行者123 更新时间:2023-11-28 07:30:58 25 4
gpt4 key购买 nike

对编码和以编程方式制作项目非常陌生。我的 Collection View 已全部设置好,但我无法弄清楚如何告诉特定单元格导航到新的 Collection View 或详细信息 View 。非常困惑和沮丧。任何人都可以帮助我或至少指出我正确的方向。请把它调低一点哈哈。

这是我的主视图 Controller

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")


}

}

这是我的收藏 View 的 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?

}

主视图 Controller 的底部代码确实打印出一个“选定”,但它为每个单元格打印它……我假设每个单元格都需要自己的 viewController.swift 文件?然后告诉那个单元格到那个swift文件来显示内容??谢谢你的时间。

最佳答案

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

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

enter image description here

我所做的是在 Storyboard中添加了 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

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

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

关于ios - 选择 Collection View 中的单元格到新 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54726560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com