Reputation: 810
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 :
collectionView cellForItemAt not being called
cellForItemAt is not calling in collectionView
cellForItemAt never called in a class extends UICollectionViewController
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 also set the datasource and collectionViewDelegate methods as self.
I have been searching for quite sometime to find an answer. Any help is really appreciated
Upvotes: 0
Views: 194
Reputation: 4669
In your updateItemData
method you are parsing your JSON
, but I don't see that you would be adding those items to any datasource object. You just iterate through the collection but do nothing with it, so when you reload the collectionView
your data source is still empty. Make sure you add the items to your itemArray
:
func updateItemData (json : JSON) {
itemArray = []
for i in 0...json["data"].count - 1{
let item = Item()
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
itemArray.append(item)
}
// useItemsCell = true
self.collectionViewHome.reloadData()
}
Also if your API callback happens on non-main thread make sure you dispatch collectionViewHome.reloadData()
to main one like someone mentioned in a comment.
Upvotes: 1