如何修复UICollectionView单元中的致命错误?

时间:2019-06-30 16:19:33

标签: ios swift uicollectionview

我在MasterViewController中有UICollectionViewstoryboard。我将这段代码用于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*

如何解决?

1 个答案:

答案 0 :(得分:0)

您尚未实现N初始化程序,如下所示:

class A(Base):
    __tablename__ = 'a_objs'

    id      = Column(Integer, primary_key=True)
    name    = Column(String(128))

    b_list = relationship('B', cascade='all, delete-orphan', lazy='dynamic', back_populates='a')

class B(Base):
    __tablename__ = 'b_objs'

    id      = Column(Integer, primary_key=True)
    name    = Column(String(128))

    a_id = Column(Integer, ForeignKey('a_objs.id'))
    a = relationship('A', back_populates='b_list')

    c_list = relationship('C', cascade='all, delete-orphan', order_by='asc(C.grade)', back_populates='b')


class C(Base):
    __tablename__ = 'c_objs'

    id              = Column(Integer, primary_key=True)
    grade           = Column(Float)
    name            = Column(String(128))

    b_id = Column(Integer, ForeignKey('b_objs.id'))
    b = relationship('B', back_populates='c_list')

您似乎正在从情节提要中加载某些单元格,因此您不能仅在此处使用init(coder:)

实现应该非常简单。您可以执行与required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } 初始化程序中相同的操作:

fatalError