从 scrollViewDidEndDecelerating 访问 collectionView
Access collectionView from scrollViewDidEndDecelerating
我有一个名为 dayPicker 的 UICollectionView,它可以水平滚动,并让您 select 一个月中的某一天。当用户停止滚动时 (scrollViewDidEndDecelerating),我希望应用程序在那天做一些事情,可以从单元格的标签访问。我在网上看到的答案都是这样的:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var someCell : UICollectionViewCell = collectionView.visibleCells()[0];
// Other code follows...
}
当我尝试从 scrollViewDidEndDecelerating
函数内部访问 collectionView
时,出现 Ambiguous use of collectionView
错误。当我替换我的 UICollectionView (dayPicker) 的实际名称时,它会出错并显示“在隐式展开可选值时意外发现 nil”。
我的问题是:如何从 scrollViewDidSomething
函数内部到达 collectionView
?目前我的 scrollViewDidEndDecelerating
函数在我的视图控制器中的 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 的屏幕截图:
我还有另一种方法可以让用户在当天点击,而且效果很好。尝试以滚动结尾完成体验。
Xcode11.4.1/Swift5
我想出来了,对于那些遇到这个线程寻找相同答案的人。非常感谢这里针对不同问题的回答:
缺少的成分是将 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
}
}
我有一个名为 dayPicker 的 UICollectionView,它可以水平滚动,并让您 select 一个月中的某一天。当用户停止滚动时 (scrollViewDidEndDecelerating),我希望应用程序在那天做一些事情,可以从单元格的标签访问。我在网上看到的答案都是这样的:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var someCell : UICollectionViewCell = collectionView.visibleCells()[0];
// Other code follows...
}
当我尝试从 scrollViewDidEndDecelerating
函数内部访问 collectionView
时,出现 Ambiguous use of collectionView
错误。当我替换我的 UICollectionView (dayPicker) 的实际名称时,它会出错并显示“在隐式展开可选值时意外发现 nil”。
我的问题是:如何从 scrollViewDidSomething
函数内部到达 collectionView
?目前我的 scrollViewDidEndDecelerating
函数在我的视图控制器中的 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 的屏幕截图:
我还有另一种方法可以让用户在当天点击,而且效果很好。尝试以滚动结尾完成体验。
Xcode11.4.1/Swift5
我想出来了,对于那些遇到这个线程寻找相同答案的人。非常感谢这里针对不同问题的回答:
缺少的成分是将 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
}
}