0

I have one api call, in that where i will fetch all the names and i am appending to one var to display in my collection view label.but values are not appending to my var.

here code :

var mobkam = [String]()
override func viewDidLoad() {
        super.viewDidLoad()
            self.getallLoans()

}

func getallLoans(){
        Manager.sharedInstance.getallLoans { (data, err) in
            if let _  = err{

            }else{
                if let dataa = data as? String{
                    if let dataFromString = dataa.data(using: String.Encoding.utf8, allowLossyConversion: false) {
                        let json = JSON(data: dataFromString)



                        print(json) // correctly display all names like ["1","2", etc]


                       self.mobileOprator.removeAll()
                        for (_, val) in json {


                            print(val.rawString()) // displaying the correct each items names


                            self.mobkam.append(val.rawString()!)
                        }
                    }
                }

            }
        }
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.mobkam.count
    }


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


        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! NameCollectionViewCell


        cell.NameLabel.text = self.mobkam[indexPath.item]


        return cell
    }

What i m doing wrong?. Not able to solve.Am i missed any.Please help me out.

Thanks !

david
  • 596
  • 1
  • 6
  • 27

1 Answers1

1

You just need to call collectionView.reloadData() after you have loaded all of the values in getallLoans.

When you make the API call, this is an asynchronous task that will take a little time to complete. Your collection view will have already loaded it's data source so you need to inform it that the data has changed. It will then call the CollectionViewDataSource delegate methods again and refresh the view based on the updated data.

for (_, val) in json {
    print(val.rawString()) // displaying the correct each items names
    self.mobkam.append(val.rawString()!)
}

collectionView.reloadData()
Scriptable
  • 17,875
  • 4
  • 50
  • 65
  • Oh , thanks. But one doubt....in did select method for collection view.....if i select any cell...i need to pick the name of that cell and dismiss the screen and in main screen i have one table view...i need to display that selected cell label name..how can i achive this.... – david Oct 27 '17 at 11:44
  • That is a different question, you would likely need to use a delegate to pass the information back to the main screen. It's not very easy to explain in the comments. When you reach this issue, post another question with your code and current issue and I will try to help – Scriptable Oct 27 '17 at 11:46
  • i measn, once i select any cell...should i need to save in any `userdefault` or any `var` to dismiss my screen and to display in my main view controller textfield ?? – david Oct 27 '17 at 11:47
  • no thats not the best way to do it. you setup a delegate... let me try to find you an example. maybe this one: https://stackoverflow.com/questions/25522912/passing-data-back-from-view-controllers-xcode. lots of good information in this google result: https://www.google.co.uk/search?q=delegate+pattern+swift+3 – Scriptable Oct 27 '17 at 11:51
  • I tried, but not able to slve..here my post https://stackoverflow.com/questions/46976309/dismiss-the-view-controller-which-have-navigation-controller-and-pass-the-value – david Oct 27 '17 at 13:34