在隐式展开可选值 (UICollectionView) 时意外发现 nil
Unexpectedly found nil while implicitly unwrapping an Optional value (UICollectionView)
我想制作在获取新项目后重新加载其数据的集合视图。但是当我尝试重新加载集合视图时,我收到了这个错误
这是View controller的代码
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)")
}
}
我不知道如何在情节提要的集合视图中正确地重新加载数据,所以如果你能帮我解决这个问题,我将非常感激
您已强制转换为零的值。我相信你需要使用 as
或 as?
。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? PhotoCollectionViewCell
...
}
也许你的 GalleryViewController
是在没有 xib/storyboard 的情况下初始化的。
如果您使用的是故事板,请试试这个:
let storyboard = UIStoryboard(name: 'yourStoryboardName', bundle: .main)
let controller = storyboard.instantiateViewController(identifier: "yourControllerIdentifier") as! GalleryViewController
如果您使用的是 xib,试试这个:
let controller = UIViewController(nibName: "yourControllerNibName", bundle: .main) as! GalleryViewController
Set identifier in Attributes Inspector
注册您的自定义单元格。
@IBOutlet private var collectionView: UICollectionView! {
didSet {
collectionView.register(PhotoCollectionViewCell.self,
forCellWithReuseIdentifier: reuseIdentifier)
}
}
我想制作在获取新项目后重新加载其数据的集合视图。但是当我尝试重新加载集合视图时,我收到了这个错误
这是View controller的代码
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)")
}
}
我不知道如何在情节提要的集合视图中正确地重新加载数据,所以如果你能帮我解决这个问题,我将非常感激
您已强制转换为零的值。我相信你需要使用 as
或 as?
。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? PhotoCollectionViewCell
...
}
也许你的 GalleryViewController
是在没有 xib/storyboard 的情况下初始化的。
如果您使用的是故事板,请试试这个:
let storyboard = UIStoryboard(name: 'yourStoryboardName', bundle: .main)
let controller = storyboard.instantiateViewController(identifier: "yourControllerIdentifier") as! GalleryViewController
如果您使用的是 xib,试试这个:
let controller = UIViewController(nibName: "yourControllerNibName", bundle: .main) as! GalleryViewController
Set identifier in Attributes Inspector
注册您的自定义单元格。
@IBOutlet private var collectionView: UICollectionView! { didSet { collectionView.register(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) } }