选择 collection 视图中的单元格到新控制器
Choose cell in collection view to new controller
对编码和以编程方式制作项目非常陌生。我已将 collection 视图全部设置好,但我无法弄清楚如何告诉特定单元格导航到新的 collection 视图或详细信息视图。非常困惑和沮丧。任何人都可以帮助我或至少指出我正确的方向。请小声点哈哈
这是我的主视图控制器
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")
}
}
这是我的 collection 视图 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?
}
主视图控制器的底部代码确实打印出 "selected" 但它会为每个单元格打印它...我假设每个单元格都需要自己的 viewController.swift 文件?然后告诉那个 swift 文件的单元格来显示内容?谢谢你的时间。
不,您不需要为每个单元格创建自己的 viewController.swift
文件。您只需要创建一个详细信息页面屏幕(一个 DetailViewController.swift)并且您需要使用在详细信息视图控制器上声明的变量传递所选单元格的详细信息。
就像我在演示项目中使用您的代码所做的那样 HERE,结果将是:
我所做的是在故事板中添加了 detailViewController 并添加了 class 文件。 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
传递数据(这里我传递选定的索引)因为 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
}
对编码和以编程方式制作项目非常陌生。我已将 collection 视图全部设置好,但我无法弄清楚如何告诉特定单元格导航到新的 collection 视图或详细信息视图。非常困惑和沮丧。任何人都可以帮助我或至少指出我正确的方向。请小声点哈哈
这是我的主视图控制器
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")
}
}
这是我的 collection 视图 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?
}
主视图控制器的底部代码确实打印出 "selected" 但它会为每个单元格打印它...我假设每个单元格都需要自己的 viewController.swift 文件?然后告诉那个 swift 文件的单元格来显示内容?谢谢你的时间。
不,您不需要为每个单元格创建自己的 viewController.swift
文件。您只需要创建一个详细信息页面屏幕(一个 DetailViewController.swift)并且您需要使用在详细信息视图控制器上声明的变量传递所选单元格的详细信息。
就像我在演示项目中使用您的代码所做的那样 HERE,结果将是:
我所做的是在故事板中添加了 detailViewController 并添加了 class 文件。 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
传递数据(这里我传递选定的索引)因为 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
}