问题
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.
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)
}
let me know if I need to add more code in please!
回答1:
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.
来源:https://stackoverflow.com/questions/59889660/ambiguous-reference-to-member-collectionview-layoutminimumlinespacingforsect