【问题标题】:How to pass data from collection view to table view class?如何将数据从集合视图传递到表视图类?
【发布时间】:2017-10-14 05:11:42
【问题描述】:

这里我有一个模型类,我需要将选定的 sku 值从集合视图传递到另一个类中的表视图,谁能帮我实现这个?

这里是didselect item at index path方法的代码

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

    // Here you can check which Collection View is triggering the segue
    if collectionView == firstCategory {

    } else if collectionView == secondCategory {
        // from other CollectionView
    }else if collectionView == newCollection{
        newPassedSku = newModel[indexPath.item].sku as? String
        print(newPassedSku)
    }else{

    }
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "firstCategorySegue" {
        _ = segue.destination as! ProductListViewController
    }
    else if segue.identifier == "secondCategorySegue" {
        _ = segue.destination as! ProductListViewController
    }else if segue.identifier == "newSegue"{
        let detailsViewController = segue.destination as! ProductDetailsViewController
        detailsViewController.index = newPassedSku
        print(newPassedSku)
    }
    else {
        _ = segue.destination as! ProductDetailsViewController
    }
}

【问题讨论】:

  • func prepare(for segue 被调用?
  • 它正在调用但无法传递我的价值@Mr.Bista
  • if segue.identifier == "newSegue"{ 条件满足?
  • 我尝试了一个新代码,但是在调用func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)之前发生了这种情况,它首先调用override func prepare(for segue: UIStoryboardSegue, sender: Any?),所以在其中传递了零值@Mr.Bista
  • 是的,它满足@Mr.Bista

标签: ios swift3 uicollectionview


【解决方案1】:

你需要直接从你的collectionview控制器到tableview控制器,你从collectionview单元调用它,因此首先调用prepareforsegue

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == newCollection{
    newPassedSku = newModel[indexPath.item].sku as? String
    self.performSegue(withIdentifier: "secondCategorySegue",
                      sender: nil)
}

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "newSegue"{
    let detailsViewController = segue.destination as! ProductDetailsViewController
    detailsViewController.index = newPassedSku
}

}

【讨论】:

  • 它正在传递 nil 值
  • 但是在这个视图控制器中我有 4 个集合视图和一个视图,所以我怎么能像你说的那样通过 segue
  • @user 提供您的故事板图片
【解决方案2】:

您已经从 Collection Cell 而不是 ViewController 进行了直接 segue。这就是为什么在func collectionView(_ collectionView: UICollectionView, didSelectItemAt 之前调用override func prepare(for segue: UIStoryboardSegue, sender: Any?) 方法的原因。

所以你基本上确实需要从 View Controller 转到下一个 View Controller。

然后您必须手动调用 segue,例如: self.prepare(for: "segueIdentifier", any: nil)func collectionView(_ collectionView: UICollectionView, didSelectIt 方法内。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView == newCollection{
        newPassedSku = newModel[indexPath.item].sku as? String
        print(newPassedSku)
        self.prepare(for: "segueIdentifier", sender: newPassedSku)
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "newSegue"{
        guard index = sender as? String else {
            return //do not segue if index is not valid
        }
        let detailsViewController = segue.destination as! ProductDetailsViewController
        detailsViewController.index = index
        print(newPassedSku)
    }
}

【讨论】:

  • 但是这里怎么传值呢?
【解决方案3】:

当您需要从模型类传递数据时完美运行

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "firstCategorySegue" {
            _ = segue.destination as! ProductListViewController
        }
        else if segue.identifier == "secondCategorySegue" {
            _ = segue.destination as! ProductListViewController
        }else if segue.identifier == "newSegue"{
            let detailsViewController = segue.destination as! ProductDetailsViewController
            let indexPaths = self.newCollection.indexPathsForSelectedItems
            let indexPath = indexPaths?[0]
            let obj = newModel[(indexPath?.row)!]
            detailsViewController.index = obj.sku as! String
            print(obj.sku)
        }
        else {
            _ = segue.destination as! ProductDetailsViewController
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多