ios - UICollectionView.setContentOffset(...) 导致 NSRangeException
问题描述
我有一个UICollectionView
代表日历,加载时生成 13 个月的数据,每个月都有一个部分。UI 中有一个按钮可以在当前年份和下一年之间切换,但是当从集合的末尾滚动回当前年份的开头时,应用程序崩溃并出现以下错误。
'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 9223372036854775806 beyond bounds [0 .. 4]'
这是导致错误的方法。
public func scrollTo(year: Int) {
if let month = months.firstIndex(where: { $0.components.year == year }),
let attributes = collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: IndexPath(item: 0, section: month)){
let y = attributes.frame.origin.y - collectionView.contentInset.top
// This is the line causing the error
collectionView.setContentOffset(CGPoint(x: 0, y: y), animated: true)
}
}
我试过打印出我的计算y
值,但0
每次崩溃都是如此。
可能值得注意的是,例外中提到的索引等于Int.max - 1
,尽管我不知道这是如何相关的y
。CGFloat
解决方案
我不确定动画是否导致崩溃,但在调用中更改animated
为解决问题。false
setContentOffset(...)
推荐阅读
- java - 如何找到 MP3 音频剪辑的标题?
- python-3.x - Python 3 编码问题:UnicodeEncodeError: 'ascii' codec can't encode character '\\xed'
- sed - 这个 sed 命令:“sed -e :a -e '$d;N;2,10ba' -e 'P;D'” 是如何工作的?
- typo3 - 在 TYPO3 9.5.5 中为表单提交操作配置静态路由
- database - 每当更新数据库时,如何刷新表单?
- angular6 - 如何从 WebApi 在 Angular6 调度程序中的 Syncfusion 中填充数据?
- javascript - `new mongoose.Schema` 每次都使用相同的默认值
- string - 按位置将字符串拆分为元胞数组
- java - 使用java配置将sql/hql注入spring bean
- javascript - 如何修复 Slimmenu 多级子项目走出屏幕