mick1996
mick1996

Reputation: 606

Ambiguous reference to member 'collectionView(_:layout:minimumLineSpacingForSectionAt:)'

I have found an error in my code that is really annoying me. I coded Swiping View Controller in a separate application and now I am trying to integrate it into this new application.

here is how view controller class is layed out:

class SwipingController: UIViewController, UICollectionViewDelegateFlowLayout {

Basically the error is as follows:

Ambiguous reference to member 'collectionView(_:layout:minimumLineSpacingForSectionAt:)'

here is some of my code, let me know if I need to add more!!

extension SwipingController {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pages.count
    }

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PageCell

        let page = pages[indexPath.item]
        //let page = pages[indexPath.row]
        cell.page = page
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height)
    }

}

the error happens on every mention of "CollectionView" in the main view controller

 @objc private func handleNext() {
        let nextIndex = min(pageControl.currentPage + 1, pages.count - 1)
        let indexPath = IndexPath(item: nextIndex, section: 0)
        pageControl.currentPage = nextIndex
        collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    }

I am also now getting the error:

Value of type 'SwipingController' has no member 'collectionViewLayout'

Which happens on this page:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

        coordinator.animate(alongsideTransition: { (_) in
            self.collectionViewLayout.invalidateLayout()

            if self.pageControl.currentPage == 0 {
                self.collectionView?.contentOffset = .zero
            } else {
                let indexPath = IndexPath(item: self.pageControl.currentPage, section: 0)
                self.collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
            }

        }) { (_) in

        }
    }

let me know if I need to add more code in please!

Upvotes: 0

Views: 370

Answers (2)

mick1996
mick1996

Reputation: 606

I managed to solve this by wrestling with it for a few hours:

My major problem was the view controller in the storyboard was a view controller and not a collection view controller.

Thank you to all who helped!

Upvotes: 0

misu
misu

Reputation: 11

The problem appears because you are trying to use the variable collectionView in your ViewController when this are not declare yet, to solve this you need to declare it as follows.

Swift 5.x:

@IBOutlet weak var collectionView: UICollectionView?

This generates an IBOutlet which you can use to connect the variable created with the UICollectionView from your storyboard. Also don’t forget to connect the delegate and the dataSource with ViewController that will implement it.

func numberOfSections(in collectionView: UICollectionView) -> Int

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

For more information about how to implement the UICollectionView click here:

As a personal recommendation I would say at the moment you declare UI variables, as UIView, UILabel or in this case an UICollectionView, use prefix to maintain an order and suitable nomenclature.

For example:

@IBOutlet weak var clvPages: UICollectionView?

Hope this helps you, I’ll stay tuned to your comments.

Upvotes: 1

Related Questions