Home > Enterprise >  collecitonview is not loaded at first
collecitonview is not loaded at first

Time:02-19

I've collection view on my third tab, whenever I log in to home page and go to third tab( where colleciton view is located ) I can't see the colleciton view, after re doing it ( going to anyother page then back to third tab ) I see my collectionview, I don't understand what the problem is, I tried sending notification when user logins and transfers to homepage and reload data of collection view, but it still doesn't work, any ideas to fix it?

collection view is configured from api, properties of that colleciton view cell's comes from api, can that be the problem ?

    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        getAPI()
        //collectionView.reloadData()
        collectionView.delegate = self
        collectionView.dataSource = self
        NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("userLoggedIn"), object: nil)
    }
    
    @objc func methodOfReceivedNotification(notification: Notification) {
        self.collectionView.reloadData()
    }

private func getAPI (){
                    APIServicies.getAirports(completion: { result in
                        switch result {
                        case .success(let airports):
                            self.airportsInformation.append(contentsOf: airports.data)
                        case .failure(let error):
                            print(error)
                        }
                        
                    })
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "searchCell", for: indexPath) as! searchCollectionViewCell
            cell.config(data: arrayToLookCellnBackDropCooler[indexPath.row],dataTwo: airportsInformation[indexPath.row])
            return cell
        }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return airportsInformation.count
    }
    

I also tried to reload data where API is read in view controller (inside getAPI function), but xCode returns purple error, saying that behaviour must be behaved in main thread, I think that is the best idea to put collection view reloadData function inside getAPI, but purple warning doesn't let me do it.

CodePudding user response:

Set your collectionview delegate and data source before calling API or you can set these from API result and then reload your collectionview. The first time your data source and delegate is not set up.

CodePudding user response:

Setup the delegate before API call.

override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
        getAPI()
        NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("userLoggedIn"), object: nil)
    }
  • Related