如何从带有Swift的2个CollectionViews的UIViewController解析数据

时间:2019-05-06 20:44:48

标签: ios swift uicollectionview

如何从具有2个不同UICollectionViews的UIViewController解析数据? 我在UIViewController上有2个UICollectionViews,我已经能够将数据从服务器拉到视图中并显示出来,但是我无法将其解析到下一个屏幕。

我使用didSelectItemAt和performSegue感到很累,但是它无法将数据解析到另一个屏幕上

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

        if collectionView == self.categoryView {
            let categoryViewController = self.storyboard?.instantiateViewController(withIdentifier: "CategoryListViewController") as! CategoryListViewController
            categoryViewController.self.selectedC = self.category[indexPath.row]
            self.navigationController?.pushViewController(categoryViewController, animated: true)

        } else if collectionView == self.featuredView {
            self.selectedPro = self.property[indexPath.row]
            self.performSegue(withIdentifier: "showDet", sender: self)
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "showCat"{
            let categoryController = segue.destination as! CategoryListViewController
            categoryController.selectedC = self.selectedC
        }
        else {
            let detailViewController = segue.destination as! DetailsViewController
            detailViewController.selectedPro = self.selectedPro
        }


    }

我希望将数据解析到第二个屏幕

2 个答案:

答案 0 :(得分:0)

为什么要使用两个不同的集合视图?为什么不创建两个不同的单元呢?这样一来,您就可以轻松地在didSelect中执行if设置,以窃听哪种类型的单元格并传递数据?

答案 1 :(得分:0)

这是解决方案

@IBOutlet weak var categoryView: UICollectionView!
let collectionViewAIdentifier = "catCell"
var category = [Category]()

@IBOutlet weak var featuredView: UICollectionView!
let collectionViewBIdentifier = "feaCell"
var property = [Property]()

override func viewDidLoad() {
    super.viewDidLoad()

    SVProgressHUD.show(withStatus: "Loading...")

    categoryView.delegate = self
    categoryView.dataSource = self
    self.view.addSubview(categoryView)

    featuredView.delegate = self
    featuredView.dataSource = self
    self.view.addSubview(featuredView)
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)     -> Int {
    var count:Int?
    // Category Collection View
    if collectionView == self.categoryView {count = category.count}

    //Property Collection View
    if collectionView == self.featuredView {count =  property.count}
    return count!
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)     -> UICollectionViewCell {
    // Category Collection View
    if collectionView == self.categoryView {
        let cellA = collectionView.dequeueReusableCell(withReuseIdentifier:     collectionViewAIdentifier, for: indexPath) as! HomeCollectionViewCell

        let cat: Category
        cat = category[indexPath.row]

        cellA.homId.text = cat.idd

        cellA.homName.text = cat.name


        //idCat = cat.idd!
        return cellA
    }
        //Property Collection View
    else {
        let cellB = collectionView.dequeueReusableCell(withReuseIdentifier:     collectionViewBIdentifier, for: indexPath) as! FeaturedCollectionViewCell

        let pro: Property
        pro = property[indexPath.row]

        cellB.proId.text = pro.idd
        cellB.proName.text = pro.name

        return cellB
    }

}

//Parsing data to next screen
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    // Category Collection View parsing data
    if segue.identifier == "showCat" {
        let categoryDetailVC = segue.destination as! CategoryListViewController
        let cell = sender as! UICollectionViewCell
        let indexPath = self.categoryView.indexPath(for: cell)
        let categoryId = category[(indexPath?.row)!].idd
        categoryDetailVC.idString = categoryId
    }

        //Property Collection View parsing data
    else if segue.identifier == "showDet" {
        let detailVC = segue.destination as! DetailsViewController
        let cell = sender as! UICollectionViewCell
        let indexPath = self.featuredView.indexPath(for: cell)
        let detailId = property[(indexPath?.row)!].idd
        detailVC.idProString = detailId
    }
}