首页 > 解决方案 > 对成员 'collectionView(_:layout:minimumLineSpacingForSectionAt:)' 的模糊引用

问题描述

我在我的代码中发现了一个让我非常恼火的错误。我在一个单独的应用程序中编写了滑动视图控制器,现在我正试图将它集成到这个新应用程序中。

这是视图控制器类的布局方式:

class SwipingController: UIViewController, UICollectionViewDelegateFlowLayout {

基本上错误如下:

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

这是我的一些代码,如果我需要添加更多,请告诉我!!

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)
    }

}

在主视图控制器中每次提到“CollectionView”时都会发生错误

 @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)
    }

我现在也收到错误:

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

发生在此页面上:

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

        }
    }

如果我需要添加更多代码,请告诉我!

标签: iosswiftxcodeuicollectionview

解决方案


出现问题是因为您试图collectionViewViewController尚未声明时使用变量,要解决此问题,您需要按如下方式声明它。

斯威夫特 5.x:

@IBOutlet weak var collectionView: UICollectionView?

这会生成一个IBOutlet可用于连接使用UICollectionView情节提要创建的变量的变量。也不要忘记连接delegatedataSourceViewController实现它。

func numberOfSections(in collectionView: UICollectionView) -> Int

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

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

有关如何实施的更多信息,UICollectionView请单击此处

作为个人建议,我会在您声明 UI 变量时说,as UIViewUILabel或者在这种情况下UICollectionView,使用前缀来维护顺序和合适的命名法。

例如:

@IBOutlet weak var clvPages: UICollectionView?

希望这对你有帮助,我会继续关注你的评论。


推荐阅读