I know they are nil because I tried to debug them and saw the values were nil so that was why they are not displaying. How can I display them then?
ImageController is constant that takes the function fetchItemArtwork which takes a url and turns it into a UIImage. CurrentPlayer is my class with the values for update. This is my ViewController
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collections: UICollectionView!
@IBOutlet weak var theSearch: UISearchBar!
var cPlayerArr = [CurrentPlayer]()
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewLayout()
downloadJSON {
self.collections.reloadData()
//print("***\(self.cPlayerArr)***")
}
self.collections.register(CurrentPlayerCell.self, forCellWithReuseIdentifier: "playercell")
self.collections.delegate = self
self.collections.dataSource = self
}
func downloadJSON(completed: @escaping () -> ()) {
let url = URL(string: "https://api.sportsdata.io/v3/nba/scores/json/Players?key=apiKey")
URLSession.shared.dataTask(with: url!) {
(data, response, error) in
if error == nil {
do {
//put data in here
self.cPlayerArr = try JSONDecoder().decode([CurrentPlayer].self, from: data!)
DispatchQueue.main.async {
completed()
}
} catch {
print("JSON Error")
}
}
} .resume()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(cPlayerArr.count)
return cPlayerArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "playercell", for: indexPath) as! CurrentPlayerCell
let item = cPlayerArr[indexPath.row]
cell.update(with: item)
return cell
}