I probably do something incredibly stupid, but I can’t understand what is happening with my life ...
What I'm trying to do is pretty simple. I have UICollectionView, depending on the data that I download from the server. After receiving and installing data from the server, I call it reloadData()as a collection. When I do this, it numberOfItemsInSectionis called with the correct count, but it is cellForItemAtnever called unless I call reloadData()the second time (I don’t even need to delay it).
Here is a general idea (although I understand that this is probably due to some arrangement in another place, but perhaps this will be enough to start the conversation):
class MyViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
var data = [DataModel]()
override func viewDidLoad() {
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib(nibName: "MyCell", bundle: nil), forCellWithReuseIdentifier: "MyCell")
getData()
}
func getData() {
someAsyncMethodToGetData() { (data) in
DispatchQueue.main.async {
self.data = data
self.collectionView.reloadData()
self.collectionView.reloadData()
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = feedCollectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as? MyCell else {
return UICollectionViewCell()
}
return cell
}
}
So, here are some things I've already tried:
reloadData() ( Thread.isMainThread )reloadData() , - - , .performBatchUpdates SO, .setNeedsLayout setNeedsDisplay ( , , .
? 🙏
1
, , , . reloadData, . , 1 getData(). getData() reloadData(), 1 ( 10 , 1 1). , , reloadData() , , , . , reloadData() , ? 🤔
, , " " .