Ios 从ScrollViewDiEndDecelling访问collectionView

Ios 从ScrollViewDiEndDecelling访问collectionView,ios,swift,uicollectionview,uiscrollview,Ios,Swift,Uicollectionview,Uiscrollview,我有一个名为dayPicker的UICollectionView,它可以水平滚动,让您选择一个月的哪一天。当用户停止滚动(ScrollViewDiEndDecelling)时,我希望应用程序在当天做一些事情,可以从单元格标签访问。我在网上看到的所有答案都与此类似: 当我试图从ScrollViewDiEndDecelling函数内部访问collectionView时,我得到了一个使用collectionView不明确的错误。当我替换UICollectionView(dayPicker)的实际名称时

我有一个名为dayPicker的UICollectionView,它可以水平滚动,让您选择一个月的哪一天。当用户停止滚动(ScrollViewDiEndDecelling)时,我希望应用程序在当天做一些事情,可以从单元格标签访问。我在网上看到的所有答案都与此类似:

当我试图从
ScrollViewDiEndDecelling
函数内部访问
collectionView
时,我得到了一个
使用collectionView
不明确的错误。当我替换UICollectionView(dayPicker)的实际名称时,它会出错为“在隐式展开可选值时意外找到nil”

我的问题是:如何从
scrollViewDidSomething
函数内部访问
collectionView
?目前,我的
ScrollViewDiEndDecreating
函数位于我的视图控制器中的
UICollectionViewDelegate
中,我还尝试将其放入
UIScrollViewDelegate
扩展中

当前代码:

extension PageOneViewController: UICollectionViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let centerPoint = CGPoint(x: UIScreen.main.bounds.midX, y: scrollView.frame.midY)
        print(centerPoint) // Successfully prints the same center point every time scrolling stops
        let indexPath = collectionView.indexPathForItem(at: centerPoint) // Ambiguous error
        let indexPath = dayPicker.indexPathForItem(at: centerPoint) // Fatal error
    }
}
有关可滚动UICollectionView的屏幕截图:

我也有另一种方法,当用户点击当天,它是完美的工作。尝试完成滚动结束的体验


Xcode 11.4.1/Swift 5

对于那些遇到这个问题并寻求相同答案的人,我找到了答案。非常感谢这里对另一个问题的回答:

缺少的内容是将
scrollView
转换为
UICollectionView
,以便您可以访问collectionView的单元格属性

工作代码:

extension PageOneViewController: UICollectionViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let collectionView = scrollView as! UICollectionView // This is crucial
        
        let centerPoint = CGPoint(x: UIScreen.main.bounds.midX, y: scrollView.frame.midY)
        let indexPath = collectionView.indexPathForItem(at: centerPoint)
        let centerCell = collectionView.cellForItem(at: indexPath!) as! MyCustomCell
        let selectedDay = centerCell.dayLabel.text //
        print(selectedDay) // Prints the value of the day in the center of the collectionView, as a string
    }
}

您是否尝试过self.collectionView?否则,您可能建议通过在扩展中设置变量来维护对
collectionView
的强引用。听起来您试图访问超出范围的对象。。。使用断点逐步遍历代码并计算变量。“如果你没有发现这样的错误,试着整理一份报告来获得更多的帮助。”SanzioAngeli提出了一个很好的建议,不幸的是,我确实尝试过,但没有成功。我还尝试在扩展中定义它,但是Xcode告诉我不能在扩展中定义变量?找到另一种方法,请参见下面的答案。谢谢你的帮助!
extension PageOneViewController: UICollectionViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let collectionView = scrollView as! UICollectionView // This is crucial
        
        let centerPoint = CGPoint(x: UIScreen.main.bounds.midX, y: scrollView.frame.midY)
        let indexPath = collectionView.indexPathForItem(at: centerPoint)
        let centerCell = collectionView.cellForItem(at: indexPath!) as! MyCustomCell
        let selectedDay = centerCell.dayLabel.text //
        print(selectedDay) // Prints the value of the day in the center of the collectionView, as a string
    }
}