如何将数据从单元格传递到另一个单元格 ViewController
how to pass data from cell to another ViewController
@TusharMordiya Please check this image
我在 CollectionView.The 中创建了一个 tableView,视图的内容是 UIImage 和一个 UILabel.I 想要设计的单元格,当我点击一个单元格时,图像和标签必须转到另一个 ViewController.
import UIKit
class ExploreTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
@IBOutlet var collectionView: UICollectionView!
var namearr = ["images-1", "images-2", "images-3", "images-4", "images-5"]
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 132, height: 134)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UIStoryboard(name: "DetailViewController", bundle: nil).instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController
vc?.name = namearr[indexPath.row]
vc?.img.image = UIImage(named: namearr[indexPath.row])
//self.navigationController?.pushViewController(vc, animated: true)
// showing error in here and while declaring object of viewcontroller
}
}
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController
将故事板名称更改为 Main
请试试这个。
添加获取最顶层视图控制器的功能
extension UIApplication {
class func topViewController(_ viewController: UIViewController? = UIApplication.shared.connectedScenes
.filter({[=10=].activationState == .foregroundActive})
.compactMap({[=10=] as? UIWindowScene})
.first?.windows
.filter({[=10=].isKeyWindow}).first?.rootViewController) -> UIViewController? {
if let nav = viewController as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = viewController as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = viewController?.presentedViewController {
return topViewController(presented)
}
return viewController
}
}
在您的 didSelectItemAt 方法中使用此代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyBoard.instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController {
vc.name = namearr[indexPath.row]
print("You tapped the cell\(indexPath) with car name \(namearr[indexPath.row]) ")
UIApplication.topViewController()?.navigationController?.pushViewController(vc, animated: true)
}
}
在您的详细信息屏幕中
class DetailViewController: UIViewController {
@IBOutlet var lbl: UILabel!
@IBOutlet var img: UIImageView!
var name = ""
override func viewDidLoad() {
super.viewDidLoad()
lbl.text = name
img.image = UIImage(named: name)
}
}
@TusharMordiya Please check this image 我在 CollectionView.The 中创建了一个 tableView,视图的内容是 UIImage 和一个 UILabel.I 想要设计的单元格,当我点击一个单元格时,图像和标签必须转到另一个 ViewController.
import UIKit
class ExploreTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
@IBOutlet var collectionView: UICollectionView!
var namearr = ["images-1", "images-2", "images-3", "images-4", "images-5"]
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 132, height: 134)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = UIStoryboard(name: "DetailViewController", bundle: nil).instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController
vc?.name = namearr[indexPath.row]
vc?.img.image = UIImage(named: namearr[indexPath.row])
//self.navigationController?.pushViewController(vc, animated: true)
// showing error in here and while declaring object of viewcontroller
}
}
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController
将故事板名称更改为 Main
请试试这个。
添加获取最顶层视图控制器的功能
extension UIApplication { class func topViewController(_ viewController: UIViewController? = UIApplication.shared.connectedScenes .filter({[=10=].activationState == .foregroundActive}) .compactMap({[=10=] as? UIWindowScene}) .first?.windows .filter({[=10=].isKeyWindow}).first?.rootViewController) -> UIViewController? { if let nav = viewController as? UINavigationController { return topViewController(nav.visibleViewController) } if let tab = viewController as? UITabBarController { if let selected = tab.selectedViewController { return topViewController(selected) } } if let presented = viewController?.presentedViewController { return topViewController(presented) } return viewController } }
在您的 didSelectItemAt 方法中使用此代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let storyBoard = UIStoryboard(name: "Main", bundle: nil) if let vc = storyBoard.instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController { vc.name = namearr[indexPath.row] print("You tapped the cell\(indexPath) with car name \(namearr[indexPath.row]) ") UIApplication.topViewController()?.navigationController?.pushViewController(vc, animated: true) } }
在您的详细信息屏幕中
class DetailViewController: UIViewController { @IBOutlet var lbl: UILabel! @IBOutlet var img: UIImageView! var name = "" override func viewDidLoad() { super.viewDidLoad() lbl.text = name img.image = UIImage(named: name) } }