0

I have MasterViewController with UICollectionView in storyboard. I use this code for my 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!

    }

But I have this error in MasterViewCell: *Thread 1: Fatal error: init(coder:) has not been implemented*

How to fix it?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user
  • 13
  • 2

1 Answers1

0

You haven't implemented the init(coder:) initialiser, as you can see here:

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

You seem to be loading some cells from the storyboard, so you must not just use fatalError here.

The implementation should be quite simple. You can just do the same thing as what you do in the init(frame:) initialiser:

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
    }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you! It is works! But now I have error in `cell.cellImageView.image = UIImage(named: "1.jpg")` - `Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value` And I can't fix it? What is the problem? How to fix? – user Jun 30 '19 at 17:59
  • @user Is there an image view in the cell in the storyboard? If so, you need to mark `cellImageView` as `@IBOutlet` and connect the outlet. – Sweeper Jun 30 '19 at 18:10
  • I have imageView in collection view cell in storyboard. In cell class I have the code: `@IBOutlet weak var cellImageView: UIImageView!`. But I have same error - `Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value` – user Jun 30 '19 at 18:33
  • @user You did not connect the outlet in the storyboard. Right click on your cell in the storyboard. Then drag the circle next to "cellImageView" to the image view. – Sweeper Jun 30 '19 at 19:08
  • I connected the outlet in the storyboard (filled circle to the left of the line). Now I reconnect imageView. Doesn't work. – user Jun 30 '19 at 19:16
  • @user Oh well, then maybe try using a separate xib file? I always did it like this. You can start [here](https://stackoverflow.com/questions/37315466/how-to-load-custom-cell-xib-in-uicollectionview-cell-using-swift). – Sweeper Jun 30 '19 at 19:18
  • I remove this line `self.collectionView?.register(MasterViewCell.self, forCellWithReuseIdentifier: "Cell")` all work fine – user Jun 30 '19 at 20:51