【发布时间】:2021-05-12 12:18:04
【问题描述】:
我想制作一个在获取新项目后重新加载其数据的集合视图。但是当我尝试重新加载集合视图时,我得到了这个错误
这是视图控制器的代码
import UIKit
class GalleryViewController: UIViewController {
var presenter : ViewToPresenterPhotoProtocol?
@IBOutlet var collectionView: UICollectionView!
let reuseIdentifier = "cell"
override func viewDidLoad() {
super.viewDidLoad()
presenter?.viewDidLoad()
// Do any additional setup after loading the view.
}
}
extension GalleryViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.presenter?.photos?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoCollectionViewCell
cell.label.text = self.presenter?.photos?[indexPath.item].name
cell.backgroundColor = UIColor.yellow
return cell
}
// MARK: - UICollectionViewDelegate protocol
private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")
}
}
extension GalleryViewController: PresenterToViewPhotoProtocol{
func onFetchPhotoSuccess() {
self.collectionView.reloadData()
}
func onFetchPhotoFailure(error: String) {
print("View receives the response from Presenter with error: \(error)")
}
}
我不知道如何在故事板的集合视图中正确重新加载数据,所以如果您能帮我解决这个问题,我将非常感激
【问题讨论】:
-
你没有在任何地方注册
PhotoCollectionViewCell单元
标签: swift uicollectionview uikit