UICollection view is not rendering properly

52 views Asked by At

I am navigating to collection view when I select didSelectRowAt at table view. I am expecting to show collection view with configuration. But is not showing..

Here is the code for

    extension MoviesViewController {
    
            override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                let movie = viewModel.state.movies[indexPath.row]
                let viewModel = MoviesDetailsViewModel(movie: movie, apiManager: APIManager())
                let viewController = SmiliarViewController(viewModel: viewModel)
                viewModel.fetchSimilarMovie()
                self.navigationController?.pushViewController(viewController, animated: true)
            }
        }

Here is the view controller code for collection view..

import UIKit

class SmiliarViewController: UIViewController {

    private let viewModel: MoviesDetailsViewModel

    init(viewModel: MoviesDetailsViewModel) {
        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
        navigationItem.largeTitleDisplayMode = .never
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate let collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.register(SimilierMovieCell.self, forCellWithReuseIdentifier: "CompanyCell")
        cv.backgroundColor = .brown
        return cv
    }()
   
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()
        self.viewModel.updatedState = {[weak self] in
            DispatchQueue.main.async {
                self?.collectionView.reloadData()
            }
        }
        viewModel.fetchSimilarMovie()
       
    }
    
    private func setUpUI() {
        
        view.addSubview(collectionView)
        let safeArea = view.safeAreaLayoutGuide
        collectionView.topAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        collectionView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
        collectionView.heightAnchor.constraint(equalToConstant: 80).isActive = true
        
    }
}

extension SmiliarViewController: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width/2.5, height: collectionView.frame.width/2)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let items = viewModel.moviePage.count
        return items
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SimilierMovieCell.identifier, for: indexPath) as? SimilierMovieCell
        
        let listMovie = viewModel.moviePage[indexPath.row]
        print(listMovie)
        cell?.configure(listMovie)
        return cell ?? SimilierMovieCell()
    }
}

When I clicked the table view cell and I am navigating to SmiliarViewController which got the collection view . but problem is not showing with layout.

Here is thee screenshot ..

enter image description here

0

There are 0 answers