ios - 对成员 '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
}
}
如果我需要添加更多代码,请告诉我!
解决方案
出现问题是因为您试图collectionView
在ViewController
尚未声明时使用变量,要解决此问题,您需要按如下方式声明它。
斯威夫特 5.x:
@IBOutlet weak var collectionView: UICollectionView?
这会生成一个IBOutlet
可用于连接使用UICollectionView
情节提要创建的变量的变量。也不要忘记连接delegate
和dataSource
将ViewController
实现它。
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 UIView
,UILabel
或者在这种情况下UICollectionView
,使用前缀来维护顺序和合适的命名法。
例如:
@IBOutlet weak var clvPages: UICollectionView?
希望这对你有帮助,我会继续关注你的评论。
推荐阅读
- javascript - 你如何使 this.forceUpdate() 应用于 React Native 中的整个应用程序?
- apache-flink - 什么是 blobstore 文件,为什么它们会不断填满 /tmp 目录?
- excel - 如何遍历包含字符串的列的行并通过电子邮件发送这些字符串?
- typescript - 我将如何在 TypeScript 中进行动态转换的 C++ 等价?
- regex - 用正则表达式匹配第二次出现
- excel - 运行 VB 代码的 Excel 工作表在打开新工作簿时出错
- python - 如何根据python中另一列中的值移动单元格?
- node.js - npm 错误!超出最大调用堆栈大小
- python - QTableview,PySide2中单元格的背景颜色
- c++ - 符号 lstat64 上的错误,未在带有链接时间参考的文件 libkernel32.so 中定义