【问题标题】:How to pass data using collection view/ segue?如何使用集合视图/ segue 传递数据?
【发布时间】:2020-04-09 04:41:43
【问题描述】:

我试图弄清楚如何在选择集合视图单元格时成功地将数据通过 segue 传递到另一个视图控制器。当我选择一个单元格时,我会崩溃。我以前没有使用集合视图。在我执行之前,我是否正确地准备了 segue?当我选择一个单元格时,我遇到了崩溃。我的数据返回零。如果我推动视图控制器,它工作正常。

// Root view controller, contains collection view that holds different categories. When cell is selected, it should take user to n
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "pushToPosts" {

        let vc = segue.destination as? ExplorePostsVC
        if let cell = sender as? UICollectionViewCell, let indexPath = collectionView.indexPath(for: cell) {
        switch selectedSegmentIndex {
            case 0: vc?.style = femaleStyles[indexPath.row]
            case 1: vc?.style = maleStyles[indexPath.row]
                default: break

                }

            }

    }


    }


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
 self.performSegue(withIdentifier: "pushToPosts", sender: self)

    }

当我选择一个单元格时,我得到一个致命错误。所有数据在下一个视图控制器中都返回 nil。


// ExplorePostsVC

 // To pass images and label
    var styleName = ""
    var styleDetails = ""
    var styleImage: UIImage?
    var style: Style!
    var headerView: ExplorePostHeader?
    override func viewDidLoad() {
    super.viewDidLoad()

fetchPosts()
    navigationController?.navigationBar.prefersLargeTitles = false
    navigationController?.navigationItem.largeTitleDisplayMode = .never
    collectionView.delegate = self
    collectionView.dataSource = self


    }

// Fetch posts based on selected cell

    func fetchPosts() {


 self.styleDetails = style.details //Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
 self.styleName = style.name
 switch style.gender {

        case.female:

        FEMALE_STYLES_REF.child((style.childValue)).observe(.childAdded) { (snapshot) in
        let postId = snapshot.key
        Database.fetchPost(with: postId, completion: { (post) in
        self.posts.append(post)
        self.posts.sort(by: { (post1, post2) -> Bool in
        return post1.creationDate > post2.creationDate

        })

        self.collectionView?.reloadData()


                })

            }


        case .male:

        MALE_STYLES_REF.child((style.childValue)).observe(.childAdded) { (snapshot) in
        let postId = snapshot.key
        Database.fetchPost(with: postId, completion: { (post) in
        self.posts.append(post)
        self.posts.sort(by: { (post1, post2) -> Bool in
        return post1.creationDate > post2.creationDate
        })
        self.collectionView?.reloadData()

        })

            }

        }
  }

【问题讨论】:

  • print(sender)。是细胞吗?在这种情况下,最好强制解开senderdestination 以立即发现问题。如果一切都正确连接,代码一定不会崩溃。
  • @vadian 你的评论帮助我到达了我需要去的地方,我发现一些代码没有正确连接到问题中。 style 需要清理代码,谢谢!

标签: ios swift uicollectionview segue


【解决方案1】:

你应该在你的 didSelectItemAt 方法中使用类似的东西

func collectionView(_ collectionView: UICollectionView, didSelectItemAt 
indexPath: IndexPath) {

    if let cell = collectionView.cellForItem(at: indexPath) {
        self.performSegue(withIdentifier: "pushToPosts", sender: cell)
    }
}

【讨论】:

  • 如果 segue 连接到集合视图 cell(而不是视图控制器),则问题中的代码应该可以工作
猜你喜欢
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
相关资源
最近更新 更多