-1

I got problem with moving to another ViewController after click in the cell.

What I want to do:

  1. After click in the cell move to another ViewController,
  2. Send url from picked cell to second ViewController for downloading new data in it.

My CollectionView look like that:

    private func setupCollectionView() {
        
        let layoutConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
        let listLayout = UICollectionViewCompositionalLayout.list(using: layoutConfig)
        
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: listLayout)
        view.addSubview(collectionView)
        
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 0.0),
            collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0.0),
            collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0.0),
            collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0),
        ])
        
        //MARK: - registration of cell with data from ViewModel
        let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, PokemonResults> { (cell, indexPath, item) in
            
            var content = cell.defaultContentConfiguration()
            content.text = item.name
            content.secondaryText = item.url
            
            cell.contentConfiguration = content
        }
        
        dataSource = UICollectionViewDiffableDataSource<Section, PokemonResults>(collectionView: collectionView) {
            (collectionView: UICollectionView, indexPath: IndexPath, identifier: PokemonResults) -> UICollectionViewCell? in
            let cell = collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier)
                
            cell.accessories = [.disclosureIndicator()]
            
            return cell
        }
        
        setupViewModel()
        
    }

And my setupViewModel like this:

    private func setupViewModel() {
        
        pokedexListVM.$pokemons
            .receive(on: RunLoop.main)
            .sink(receiveValue: { [weak self] _ in
                self?.collectionView.reloadData()
            }).store(in: &subscriptions)
        
        self.snapshot = NSDiffableDataSourceSnapshot<Section, PokemonResults>()
        self.snapshot.appendSections([.main])
        self.snapshot.appendItems(self.pokedexListVM.pokemons, toSection: .main)
        
        dataSource.apply(snapshot, animatingDifferences: false)
}

How to use delegate in that kind of coding the CollectionView?

Jaanioo
  • 15
  • 5

1 Answers1

0

First of all let’s fix setupViewModel() a little bit.

private func setupViewModel() {
    pokedexListVM.$pokemons
        .receive(on: RunLoop.main)
        .sink(receiveValue: { [weak self] _ in
            guard let `self` = self else { return }
            self.collectionView.reloadData()
            self.snapshot = NSDiffableDataSourceSnapshot<Section, PokemonResults>()
            self.snapshot.appendSections([.main])
            self.snapshot.appendItems(self.pokedexListVM.pokemons, toSection: .main)
    
            dataSource.apply(snapshot, animatingDifferences: false)
        }).store(in: &subscriptions)
}

Secondly, let's say that the controller containing the collectionView is called CurrentViewController. Make your CurrentViewController conform to UICollectionViewDelegate and implement collectionView(_:didSelectItemAt:) , you can read about that here. It can look like that:

extension CurrentViewController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let row = indexPath.row
        yourTransitionFunction(url: pokedexListVM.pokemons[row].url)
    }
}

And don't forget to set your controller as a delegate for collectionView in setupCollectionView() like that collectionView.delegate = self

Bulat Yakupov
  • 440
  • 4
  • 13