如何将数据从集合视图传递到表视图类?

时间:2017-10-14 05:11:42

标签: ios swift3 uicollectionview

这里我有一个模型类,其中我需要将选定的sku值从集合视图传递到另一个类的表视图,任何人都可以帮助我实现这个吗?

以下是索引路径方法

中didselect项的代码
 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
    }
}

3 个答案:

答案 0 :(得分:0)

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

所以你基本上需要从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)
    }
}

答案 1 :(得分:0)

你需要直接从你的collectionview控制器到tableview控制器你的segue,你从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
}

}

答案 2 :(得分:0)

当您需要从模型类传递数据时,它可以正常工作

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