Akash More
Akash More

Reputation: 141

UICollectionViewController Calling DataSource method of another UICollectionViewController

I have implemented custom UITabBarController
First and Second UITabBarItem is a UICollectionViewController
Fourth UITabBarItem is a UIViewController

CollectionView in 1st tab and its cell is created programmatically and working perfectly with dynamic cell size.

CollectionView methods in the 2nd tab are not edited.

Problem: App launched and 1st CollectionView is loaded with a cell, then I go to 2nd Tab which is also a CollectionViewController which does not have any cell, then I go to 1st tab again and the cell on 1st UICollectionview won't display.

Things I noticed:

  1. Added debug point on collectionview methods in both VC. 1st tab VC is calling methods of 2nd VC(Strange!) after I move from 2nd tab to 1st.
  2. Going to Fourth tab (UIViewController) and then back to 1st does not cause any problem.

Solution I tried:

  1. Register cell of 1st VC in viewWillAppear (it used to crash at dequeueReusableCell but now doesn't crash after assigning tag and checking it in cellForItemAt)
  2. Assign tag to both collectionview in both VC and check for collectioniView.tag in methods of both VC, but 1st tab VC is still calling methods in 2nd tab VC

Edit 1:
called in viewWillAppear()

SetupCollectionView(){
  collectionView?.register(VideoCell.self, forCellWithReuseIdentifier: cellId)
        self.collectionView?.delegate = self
        self.collectionView?.dataSource = self
}  

CollectionView Methods in 1st VC

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            if collectionView.tag == 339 {
                print("videos.count: \(self.videos.count)")
                if videos.count > 0 {
                    return self.videos.count
                }else {
                    return 0
                }
            }
            return 0
        }

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            if collectionView.tag == 339 {
                //setup cell to display
            }else{
                let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
                return cell2
            }
        }

same check is performed in sizeForItemAt and 2nd VC.

Edit 2:

  1. in AppDelegate

    self.window?.rootViewController = CustomTabBarController()

  2. in CustomTabBarController

1st VC

        let layout = UICollectionViewFlowLayout()
        let homeController = HomeControllerMain(collectionViewLayout: layout)
        let homeViewNavController = UINavigationController(rootViewController: homeController)
        homeViewNavController.tabBarItem.title = nil
        homeViewNavController.tabBarItem.image = UIImage(named: "home1")
        homeViewNavController.tabBarItem.selectedImage = UIImage(named: "home")
        homeViewNavController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

2nd VC

let commentController = searchMainViewController(collectionViewLayout: layout)
let commentViewNavController = UINavigationController(rootViewController: commentController)
commentViewNavController.tabBarItem.title = ""
commentViewNavController.tabBarItem.image = UIImage(named: "search1")
commentViewNavController.tabBarItem.selectedImage = UIImage(named: "search")?.withRenderingMode(.alwaysOriginal)
commentViewNavController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

Upvotes: 0

Views: 185

Answers (1)

Akash More
Akash More

Reputation: 141

Solved!
I was using single UICollectionViewFlowLayout variable in both homeController and commentController.

Created second UICollectionViewFlowLayout variable for commentController and problem solved.

Upvotes: 0

Related Questions