Swift Unable to Use API Data in Collection View

unable to print response from api in collectionView

you just need to reload you collectionView - try this in your do statement

DispatchQueue.main.async() {
self.collectionView.reloadData()
}

Collection View Not Showing Data

Try instead of

nc.post(name: Notification.Name("didFinishParsing"), object: nil)

send

nc.post(name: Notification.Name("didFinishParsing"), object: news)

Then instead of

guard let news = notification.object as? Article else { return}

write

guard let news = notification.object as? News else { return}
print("News == \(news)")
self.news = news.articles

Not able to append the value and display in collection view

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()

Unable to show all JSON response values in Collectionview(ERROR: Index out of range) in Swift

First of all consolidate the naming of the variables to make clearer what is a single object and what is an array.

  • Replace bidAttatchment with attachments (plural form)
  • Replace Get_attachments with Attachment (singular form)
  • Declare the array as non-optional empty array. The benefit is you get rid of all the question marks

Then in numberOfItemsInSection just return the number of items in attachments. Your data source doesn't contain sections so the section parameter is irrelevant.

class ViewProposalTableVIewCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource {


@IBOutlet weak var attetchmentsCollectionview: UICollectionView!

public var attachments = Array<Attachment>()

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BidAttatchmentCollectionViewCell", for: indexPath) as! BidAttatchmentCollectionViewCell

let attatchBid = attachments[indexPath.item]
cell.attatchmentLbl.text = attatchBid.filename

return cell
}

Why i am not able to delete one more CollectionVIew Cell with JSON parameter in swift?

You are storing picture with same key in user defaults, which is wrong. it will only store last item of the loop.
You should add picID in your model inside postServiceCall method like this

self.arrImageItems.append(ImageItemModel(title: self.titleTextfield.text, imgTitle: self.imageProfile, picID: picId))

and then in deleteService method you need to get picID like this

let picId = UserDefaults.standard.string(forKey: "pic_id")//Replace this with this one
let picId = arrImageItems[sender.tag]. picID

Now everything will work fine.



Related Topics



Leave a reply



Submit