Hi I have been searching for this a lot and couldn't get an appropriet answer.
I am trying to populate a collectionView with data received online using an API.
Data is returned but the collectionView cellForItemaAt never runs because when I use print statements nothing gets displayed.
I can't figure out what the problem is , I looked at these tow links but they werent't helpful :
https://stackoverflow.com/questions/51298015/collectionview-cellforitemat-not-being-called
https://stackoverflow.com/questions/46853189/cellforitemat-is-not-calling-in-collectionview
here is the cellForItemAt method :
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCollectionViewCell", for: indexPath) as! ItemCollectionViewCell
cell.ItemNameLabel.text = itemArray[indexPath.row].name
print(itemArray[indexPath.row].name)
cell.priceLable.text = itemArray[indexPath.row].price + " " + itemArray[indexPath.row].currency
guard let url : URL = URL(string: itemArray[indexPath.row].image) else {return cell}
cell.imageView.sd_setShowActivityIndicatorView(true)
cell.imageView.sd_setIndicatorStyle(.gray)
cell.imageView.sd_setImage(with: url, placeholderImage: UIImage(named:"placeholderImage"), options: .refreshCached, completed: nil)
return cell
}
here is the method I use to retrieve the data :
func getAllItemsApiMethod()
{
itemArray.removeAll()
if Reachability.sharedInstance.connectedToNetwork()
{
if searchShops == true {
PostDict = ["page" : pageNumber ,"text" : searchKeyword,"searchShop" : "1"]
}else{
PostDict = ["page":"1","text":searchKeyword]
}
print(PostDict)
StartIndicator()
WebServices.getItemsMethod(url: itemsUrl, parameters: PostDict) { (JsonResponse) in
print(JsonResponse)
StopIndicator()
let json : JSON = JSON(JsonResponse)
self.updateItemData(json: json)
print(json)
}
}
else
{
FTIndicator.showToastMessage(Constant.NoInternet)
}
}
func updateItemData (json : JSON) {
let item = Item()
for i in 0...json["data"].count - 1{
item.name = json["data"][i]["title_ku"].stringValue
item.price = json["data"][i]["price"].stringValue
item.image = json["data"][i]["image"].stringValue
item.currency = json["data"][i]["currency"].stringValue
}
// useItemsCell = true
self.collectionViewHome.reloadData()
}
and here is the method i use to call the getAllItemsAPI :
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
guard let searchKeyword = txtFldSearch.text else {return false}
getAllItemsApiMethod()
collectionViewHome.reloadData()
self.view.endEditing(true)
txtFldSearch.text = ""
return true
}
here is the numberOfItems method :
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return itemArray.count
}
I have been searching for quite sometime to find an answer. Any help is really appreciated