【问题标题】:Unexpectedly found nil while implicitly unwrapping an Optional value (UICollectionView)在隐式展开可选值时意外发现 nil (UICollectionView)
【发布时间】: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


【解决方案1】:

也许你的 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

【讨论】:

    【解决方案2】:
    1. Set identifier in Attributes Inspector

    2. 注册您的自定义单元格。

       @IBOutlet private var collectionView: UICollectionView! {
           didSet {
               collectionView.register(PhotoCollectionViewCell.self,   
                                       forCellWithReuseIdentifier: reuseIdentifier)
           }
       }
      

    【讨论】:

      【解决方案3】:

      您强制转换了一个 nil 值。我相信你需要使用as 或as?。

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? PhotoCollectionViewCell
          
          ...
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2016-10-18
      相关资源
      最近更新 更多