【问题标题】:How to fix fatal error in UICollectionView cell?如何修复 UICollectionView 单元格中的致命错误?
【发布时间】:2019-11-11 13:27:03
【问题描述】:

我在storyboard 中有MasterViewControllerUICollectionView。我将此代码用于我的UICollectionView

MasterViewController:

class MasterViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, URLSessionDownloadDelegate, SKProductsRequestDelegate, SKStoreProductViewControllerDelegate {

@IBOutlet weak var collectionView: UICollectionView?

fileprivate var currentPage: Int = 0
    fileprivate var pageSize: CGSize {
        let layout = UPCarouselFlowLayout()
        var pageSize = layout.itemSize
        pageSize.width += layout.minimumLineSpacing
        return pageSize
    }

override func viewDidLoad() {
        super.viewDidLoad()

        self.addCollectionView()
        self.setupLayout()
}

func setupLayout(){

        let pointEstimator = RelativeLayoutUtilityClass(referenceFrameSize: self.view.frame.size)

        self.collectionView?.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        self.collectionView?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: pointEstimator.relativeHeight(multiplier: 0.1754)).isActive = true
        self.collectionView?.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
        self.collectionView?.heightAnchor.constraint(equalToConstant: pointEstimator.relativeHeight(multiplier: 0.6887)).isActive = true

        self.currentPage = 0
    }

    func addCollectionView(){

        let pointEstimator = RelativeLayoutUtilityClass(referenceFrameSize: self.view.frame.size)

        let layout = UPCarouselFlowLayout()

        layout.itemSize = CGSize(width: pointEstimator.relativeWidth(multiplier: 0.73333), height: 400)

        layout.scrollDirection = .horizontal

        self.collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

        self.collectionView?.translatesAutoresizingMaskIntoConstraints = false

        self.collectionView?.delegate = self
        self.collectionView?.dataSource = self

        self.collectionView?.register(MasterViewCell.self, forCellWithReuseIdentifier: "Cell")


        let spacingLayout = UPCarouselFlowLayout()
        spacingLayout.spacingMode = UPCarouselFlowLayoutSpacingMode.overlap(visibleOffset: 20)
    }

MasterViewCell:

class MasterViewCell: UICollectionViewCell {
    let customView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.layer.cornerRadius = 12
        return view
    }()
    var cellImageView: UIImageView!


    override init(frame: CGRect) {
        super.init(frame: frame)

        self.addSubview(self.customView)

        self.customView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        self.customView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        self.customView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1).isActive = true
        self.customView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1).isActive = true


    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

RelativeLayoutUtilityClass:

class RelativeLayoutUtilityClass {

    var heightFrame: CGFloat?
    var widthFrame: CGFloat?

    init(referenceFrameSize: CGSize){
        heightFrame = referenceFrameSize.height
        widthFrame = referenceFrameSize.width
    }

    func relativeHeight(multiplier: CGFloat) -> CGFloat{

        return multiplier * self.heightFrame!
    }

    func relativeWidth(multiplier: CGFloat) -> CGFloat{
        return multiplier * self.widthFrame!

    }

但我在MasterViewCell: *Thread 1: Fatal error: init(coder:) has not been implemented* 有这个错误

如何解决?

【问题讨论】:

  • 点击“修复”按钮。

标签: ios swift uicollectionview


【解决方案1】:

您尚未实现 init(coder:) 初始化程序,如您在此处看到的:

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

您似乎正在从情节提要中加载一些单元格,因此您不能只在此处使用fatalError

实现应该很简单。您可以像在 init(frame:) 初始化程序中那样做同样的事情:

class MasterViewCell: UICollectionViewCell {
    let customView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.layer.cornerRadius = 12
        return view
    }()
    var cellImageView: UIImageView!


    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    private func commonInit() {
        self.addSubview(self.customView)

        self.customView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        self.customView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        self.customView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1).isActive = true
        self.customView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1).isActive = true
    }
}

【讨论】:

  • 谢谢!这是作品!但是现在我在cell.cellImageView.image = UIImage(named: "1.jpg") - Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value 中有错误而且我无法修复它?问题是什么?如何解决?
  • @user 故事板的单元格中是否有图像视图?如果是这样,您需要将cellImageView标记为@IBOutlet并连接插座。
  • 我在情节提要的集合视图单元格中有 imageView。在单元格中,我有代码:@IBOutlet weak var cellImageView: UIImageView!。但我有同样的错误 - Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
  • @user 您没有连接情节提要中的插座。右键单击情节提要中的单元格。然后将“cellImageView”旁边的圆圈拖到图像视图中。
  • 我连接了情节提要中的插座(行左侧的实心圆圈)。现在我重新连接 imageView。没用。
猜你喜欢
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 2020-08-13
  • 2023-03-12
相关资源
最近更新 更多