Robbe Verhoest
Robbe Verhoest

Reputation: 71

JSON parsing after protocols

I implemented a carousel with a collection view. I want to parse a JSON data into it, but the JSON parses after the necessary protocols. Because of this the array is empty when i try to configure the cells.

I know the problem occurs when I try to reload the data. When I do this in a table view controller I can just call tableView.reloadData() but I can't do this in the normal view controller I use.

        self.view.setNeedsLayout()
        self.view.layoutIfNeeded()

This is the order when i print it: loadjson numberofsections cellforitemat cellforitemat parsejson

Upvotes: 0

Views: 26

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

When I do this in a table view controller I can just call tableView.reloadData() but I can't do this in the normal view controller

in normal vc you should have a property like

@IBOutlet weak var collectionView:UICollectionView!

and reload it when needed unlike the collection controller which encapsulates that property by default


Regrading the order of getting the data is asynchronous so check this CollectionView not display data after parsing JSON for a similar case


These 2 lines

self.view.setNeedsLayout()
self.view.layoutIfNeeded()

are totally irrelevant , they used for refreshing the UI and constraints

Upvotes: 1

Related Questions