【发布时间】:2020-01-24 02:24:53
【问题描述】:
我在我的代码中发现了一个让我非常恼火的错误。我在一个单独的应用程序中编写了 Swiping View Controller 的代码,现在我正在尝试将它集成到这个新应用程序中。
这是视图控制器类的布局方式:
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
}
}
如果我需要添加更多代码,请告诉我!
【问题讨论】:
-
请显示更多上下文。特别是,你声明 SwipingController 符合 UICollectionViewDelegateFlowLayout 了吗?
-
@matt 感谢您抽出宝贵时间发表评论!你能建议我应该显示什么代码吗?我还更新了问题以显示它如何符合 UICollectionViewDelegateFlowLayout
-
好吧,这很好,但你所说的仍然很神秘,因为你一直在说得很含糊,好像故事中有两个不同的视图控制器。有时有 SwipingController 有时有“主视图控制器”。它们如何相互关联?哪个持有收藏视图?如果 SwipingController 是集合视图控制器,您不能指望“主视图控制器”知道
collectionView?.scrollToItem的含义。
标签: ios swift xcode uicollectionview