我有一个项目是你登录后,图像从网上加载到一个collectionView。我将部分数设置为1,并且第一次登录时,所有内容都正确显示。 我可以看到下面的顺序:UICollectionView cellForItemAt第二次没有调用
- numberOfSection(这里给1)
- numberOfItemsInSection 0
- insetForSectionAt
然后我有一个事件时,我的图片下载我叫下面的代码didFinishDownloadingTo
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
let url=(downloadTask.currentRequest?.url)?.pathComponents
let urlId = (url!.last)!
let offerIndex = self.downloadIndex[urlId]
let imageData=try? Data(contentsOf: location)
if imageData != nil && offerIndex != nil && self.offers[offerIndex!] != nil /* && offerIndex! < self.offers.count */ {
self.stopAutoScrolling()
//Sending to background thread since we see in profile that this takes time and hanging main thread
DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async(execute: {
self.downloadCache[urlId] = imageData
self.offers[offerIndex!]!.image = UIImage(data: imageData!)
self.updateCache()
DispatchQueue.main.async {
self.setupDataForCollectionView()
self.collectionView?.reloadData()
self.startAutoScrolling()
}
})
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return self.view.frame.size
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: -64, left: 0, bottom: 0, right: 0)
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("section num \(section)")
return carousel.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! EventOfferCell
//LoggingManager.sharedInstance.logInfo("Carousel count %i indexPath %i", carousel.count,indexPath.row)
if let offer = offerForIndexPath(indexPath) {
if let offerImage = offer.image {
cell.imageView.image = offerImage
cell.activityIndicator.stopAnimating()
cell.titleLabel.isHidden = true
}
else {
cell.titleLabel.text = offer.title
}
}
return cell
}
func setupDataForCollectionView() {
carousel.removeAll()
for (_, offer) in self.offers {
carousel.append(offer)
}
if carousel.count > 1 {
let firstOffer = carousel.first
let lastOffer = carousel.last
carousel.append(firstOffer!)
carousel.insert(lastOffer!, at: 0)
}
}
在reloadDat AI得到以下情形:
- numberOfSection(这里给1)
- numberOfItemsInSection 4
- sizeForItemAt
- cellForItemAt被调用,并设置我的图片,我想滚动 之间
虽然第二次我退出和收集视图加载后,我得到相同的情况,虽然后reloadData图像下载时没有事情正在发生,没有numberOfSection什么也没有。 我真的尝试了许多不同的解决方案,但没有任何工作。 由于cellForItemAt和sizeForItemAt不叫我没有得到任何图像显示在我第二次登录
我试图numberOfItemsInSection静态设置为2,静态提供两个图像,并且正在从那时起sizeForItemAt被称为等
我的数据源和委托在故事板设置为正确的控制器
我真的卡在这个问题,让所有的帮助表示赞赏