【问题标题】:how to pass data from cell to another ViewController如何将数据从单元格传递到另一个 ViewController
【发布时间】:2021-11-19 10:47:24
【问题描述】:

@TusharMordiya Please check this image 我在 CollectionView 中创建了一个 tableView。视图的内容是 UIImage 和 UILabel。我想设计一个单元格,当我单击一个单元格时,图像和标签必须转到另一个 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
   }
}

【问题讨论】:

  • 我猜你可能想解开vc。但是显示您拥有的完整错误消息可能会有所帮助。我们可以帮助您理解它。我猜到了错误,但它可能是别的东西......
  • 我更仔细地阅读了您的代码(如果您显示错误消息,我就不需要了)。但是你在 UITableViewCell 中有一个 UICollectionView,所以这里的 selfUITableViewCell,而不是 UIViewController。您需要用closuredelegate 系统告诉UIViewController,以使用相应的数据推送新的视图控制器。
  • 我会质疑你为什么在 UITableViewCell 中有一个 UICollectionView?您是否尝试在表格视图中创建水平滚动行?如果是这种情况,我建议根本不使用 UITableView 并查看 UICollectionViewCompositionalLayout...developer.apple.com/documentation/uikit/…
  • @Larme 我在 didSelectItemAt indexPath 和实例化故事板中有错误
  • 真正的错误信息是什么?重要的是显示真实信息,而不是“伪信息”。

标签: ios swift uicollectionview pushviewcontroller


【解决方案1】:

请试试这个。

  1. 添加获取最顶层视图控制器的功能

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

【讨论】:

  • 你能说一下代码是什么意思吗
  • @JESTINSAJI 您不能在您的 collectionviewcell 中使用 self ,因此 topViewController 方法返回位于导航堆栈顶部的 viewcontroller。通过使用它,您可以导航到视图控制器。在您的代码中,您的错误是您在创建情节提要对象时编写了视图控制器标识符。你应该写故事板名称而不是视图控制器标识符
  • 它起作用了,但是 "vc.img.image = UIImage(named: namearr[indexPath.row])" 对于这一行我收到错误致命错误:在隐式展开可选值时意外发现 nil
  • @JESTINSAJI 我已经更新了我的答案。我希望这能解决您的问题
  • @TusharMordiya 当我单击单元格(图像)时,它没有导航到 detailViewController。它仅在控制台中打印该行。我评论了这条线并检查了,然后什么都没有发生
【解决方案2】:
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"DetailViewController") as? DetailViewController

将故事板名称更改为 Main

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多