Aditya Prasad
Aditya Prasad

Reputation: 1

Data Passing from CollectionView to TableView

Attempt to present <Home.ProductInfoTableViewController: 0x1031b5000> on <Home.mainCollectionViewController: 0x10305a800> (from <Home.mainCollectionViewController: 0x10305a800>) which is already presenting <Home.ProductInfoTableViewController: 0x10480d200>.

Data passing error I am facing. I don't know what's going on.

import UIKit

private let reuseIdentifier = "ItemCollectionViewCell"

class mainCollectionViewController: UICollectionViewController {

    var furnitureCategory: FurnitureCategory?
    var furnitureItems: [FurnitureItem] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationController?.navigationBar.barTintColor = .white
        collectionView.collectionViewLayout = createLayout()
        
        if let furnitureCategory = furnitureCategory {
            furnitureItems = furnitureCategory.furnitureItems
        } else {
            print("Furniture category is nil")
        }
    }
  
    // MARK: - Compositional Layout
    func createLayout() -> UICollectionViewLayout {
        let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5), heightDimension: .fractionalHeight(1.0))
        let item = NSCollectionLayoutItem(layoutSize: itemSize)
        item.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)

        let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(200))
        let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])

        let section = NSCollectionLayoutSection(group: group)
        section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)

        return UICollectionViewCompositionalLayout(section: section)
    }

    // MARK: - UICollectionViewDataSource
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return furnitureItems.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? ItemCollectionViewCell else {
            fatalError("Unable to dequeue ItemCollectionViewCell")
        }
        
        let item = furnitureItems[indexPath.item]
        configure(cell: cell, with: item)
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.isUserInteractionEnabled = false // Disable interaction
        let selectedItem = furnitureItems[indexPath.item]
        performSegue(withIdentifier: "showProductInfoSegue", sender: selectedItem)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showProductInfoSegue", let item = sender as? FurnitureItem {
            if let destinationVC = segue.destination as? ProductInfoTableViewController {
                destinationVC.furnitureItem = item
            } else {
                print("Destination view controller is not of type ProductInfoTableViewController")
            }
        } else {
            print("Invalid segue identifier or sender is not a FurnitureItem")
        }
    }
    
    func configure(cell: ItemCollectionViewCell, with item: FurnitureItem) {
        cell.ProductImg?.image = item.image ?? UIImage(named: "placeholder")
        cell.ProductName?.text = item.name
    }
}

Upvotes: -1

Views: 26

Answers (0)

Related Questions