title: How to disable specific cells in collection view in Swift
body: <p>I have created a collection view that acts as a nav bar, and I've made this the base view controller(TabBar.swift) and it will appear on all my other view controllers. it has 3 cells for now and clicking on each cell shall direct the user to its respective view controller. The 3 view Controllers are FirstScreen.swift, SecondScreen.swift and ThirdScreen.swift. Here is the code of the base view controller:</p>
  
  <p><strong>TabBar.swift</strong></p>
  
  <pre><code>struct OpenViewController {
      var viewController: UIViewController
  }
  
  class TabBar: UICollectionViewController, UICollectionViewDelegateFlowLayout {
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // Call all the elements
          setupCollectionView()
  
          view.backgroundColor = .white
  
  
      }
  
      let viewControllerList = [
  
          OpenViewController(viewController: FirstScreen()),
          OpenViewController(viewController: SecondScreen()),
          OpenViewController(viewController: ThirdScreen()),
  
      ]
  
      // Setup the collection veiw
      func setupCollectionView() {
  
          collectionView?.backgroundColor = .clear
          collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellID")
          collectionView.showsHorizontalScrollIndicator = false
  
          view.addSubview(collectionView)
          addCollectionViewConstraints()
  
      }
  
      // Add constraints to the collection view
      func addCollectionViewConstraints() {
  
          collectionView.translatesAutoresizingMaskIntoConstraints = false
          collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
          collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
          collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
          collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.3).isActive = true
  
      }
  
      // The number of elements inside the collection view
      override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 3
      }
  
      // Adding the cells
      override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)
  
          // Customize the cells
          cell.backgroundColor = .clear
          cell.layer.cornerRadius = collectionView.contentSize.height / 2
          cell.layer.borderWidth = 1
          cell.layer.borderColor = UIColor.black.cgColor
  
          return cell
      }
  
      // The witdth and the height of the cell
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
          return CGSize(width: collectionView.frame.height, height: collectionView.frame.height)
      }
  
      // Padding to the cells
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -&gt; UIEdgeInsets {
          return UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
      }
  
      // Selecting a cell and navigating to another view controller
      override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  
          let openViewController = self.viewControllerList[indexPath.row]
          self.present(openViewController.viewController, animated: true)
      }
  
  }
  </code></pre>
  
  <p><strong>FirstScreen.swift</strong></p>
  
  <pre><code>class FirstScreen: TabBar {
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          view.backgroundColor = .yellow
  
      }
  }
  </code></pre>
  
  <p><strong>SecondScreen.swift</strong></p>
  
  <pre><code>class SecondScreen: TabBar {
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          view.backgroundColor = .green
  
      }
  }
  </code></pre>
  
  <p><strong>ThirdScreen.swift</strong></p>
  
  <pre><code>class ThirdScreen: TabBar {
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          view.backgroundColor = .red
  
      }
  }
  </code></pre>
  
  <p><strong>AppDelegate.swift</strong></p>
  
  <pre><code>func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {
  
      window = UIWindow()
      window?.makeKeyAndVisible()
  
      let layout = UICollectionViewFlowLayout()
      layout.scrollDirection = .horizontal
      let firstScreen = SecondScreen(collectionViewLayout: layout)
  
      window?.rootViewController = firstScreen
  
      return true
  }
  </code></pre>
  
  <p>Now when I run this, this crashes and the reason is, for example, if the user is on FirstScreen.swift and the user clicks on the first cell which will lead the user to the FirstScreen.swift it will not work, is there a way to solve this problem? Like to disable a cell when the user is on the respective view controller.</p>