簡體   English   中英

如何將數據從單元格傳遞到另一個 ViewController

[英]how to pass data from cell to another ViewController

@TusharMordiya 請檢查這張圖片我在 CollectionView 中創建了一個 tableView。視圖的內容是 UIImage 和一個 UILabel。我想設計一個單元格,當我點擊一個單元格時,圖像和 label 必須 go 到另一個 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

將 storyBoard 名稱更改為 Main

請試試這個。

  1. 添加 function 以獲得最高視圖 controller

     extension UIApplication { class func topViewController(_ viewController: UIViewController? = UIApplication.shared.connectedScenes.filter({$0.activationState ==.foregroundActive}).compactMap({$0 as? UIWindowScene}).first?.windows.filter({$0.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 } }
  2. 在您的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) } }
  3. 在您的詳細信息屏幕中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM