实现
我想在UICollectionView
中有两个不同的项目大小。因此,我已经实施collectionView(_:layout:sizeForItemAt:)
的UICollectionViewDelegateFlowLayout
。
错误
但是我遇到的问题是,每当我访问此函数中的collectionViewLayout.collectionViewContentSize
时,我的应用程序都会崩溃并发生异常。
例外情况如下:
*由于未捕获的异常“ NSRangeException”而终止应用程序,原因:“ *-[__ NSArrayM objectAtIndexedSubscript:]:索引1超出范围[0 .. 0]”。
引发调用堆栈位于帖子的底部。
看来这是SDK的内部问题,可能某些东西没有正确初始化!
题
谁能确认这个错误或告诉我我在做什么错?
实作
这是我UICollectionViewDelegateFlowLayout
的实现:
class TwoSizesCellFlowLayout: NSObject, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let layoutSize = collectionViewLayout.collectionViewContentSize // CRASHES when accessing `collectionViewContentSize`
// SDK error?
let layoutSize = collectionView.contentSize // works but results in slightly different size
var size = layoutSize.width / 2
if let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout {
size = size - flowLayout.minimumInteritemSpacing / 2
}
return CGSize(width: size, height: size)
}
}
在集合视图控制器子类中,我将
TwoSizesCellFlowLayout
的对象分配给delegate
的UICollectionViewController
属性:class ViewController: UICollectionViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
collectionView?.dataSource = self
collectionView?.delegate = collectionViewDelegateAndLayout
let nib = UINib.init(nibName: "BasicCollectionViewCell", bundle: nil)
collectionView?.register(nib, forCellWithReuseIdentifier: cellIdentifier)
// accessing `collectionViewContentSize` here works like a charm, but only one size for all items is possible
// let collectionViewSize = collectionView?.collectionViewLayout.collectionViewContentSize
// if let collectionSize = collectionViewSize, let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
// let size = collectionSize.width / 2 - flowLayout.minimumInteritemSpacing / 2
// let cellSize = CGSize(width: size, height: size)
// flowLayout.itemSize = cellSize
// }
}
...
}
首先抛出调用堆栈:
0 CoreFoundation 0x00000001117751e6 __exceptionPreprocess + 294
1 libobjc.A.dylib 0x000000010d946031 objc_exception_throw + 48
2 CoreFoundation 0x00000001117b50bc _CFThrowFormattedException + 194
3 CoreFoundation 0x00000001117a67b1 -[__NSArrayM objectAtIndexedSubscript:] + 177
4 UIKit 0x000000010ecf2957 -[UICollectionViewFlowLayout _getSizingInfosWithExistingSizingDictionary:] + 1801
5 UIKit 0x000000010ecf47db -[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 136
6 UIKit 0x000000010eced70a -[UICollectionViewFlowLayout collectionViewContentSize] + 66
7 _blackened_ 0x000000010d28812f _T07beSmart22TwoSizesCellFlowLayoutC14collectionViewSC6CGSizeVSo012UICollectionI0C_So0kiG0C6layout10Foundation9IndexPathV13sizeForItemAttF + 79
8 _blackened_ 0x000000010d2882fa _T07beSmart22TwoSizesCellFlowLayoutC14collectionViewSC6CGSizeVSo012UICollectionI0C_So0kiG0C6layout10Foundation9IndexPathV13sizeForItemAttFTo + 122
9 UIKit 0x000000010ecf2e50 -[UICollectionViewFlowLayout _getSizingInfosWithExistingSizingDictionary:] + 3074
10 UIKit 0x000000010ecf47db -[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 136
11 UIKit 0x000000010eced104 -[UICollectionViewFlowLayout prepareLayout] + 272
12 UIKit 0x000000010ed1a0a9 -[UICollectionViewData _prepareToLoadData] + 156
13 UIKit 0x000000010ed1a956 -[UICollectionViewData validateLayoutInRect:] + 53
14 UIKit 0x000000010ecb0620 -[UICollectionView layoutSubviews] + 260
15 UIKit 0x000000010e2b17a8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1515
16 QuartzCore 0x0000000114d45456 -[CALayer layoutSublayers] + 177
17 QuartzCore 0x0000000114d49667 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
18 QuartzCore 0x0000000114cd00fb _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 343
19 QuartzCore 0x0000000114cfd79c _ZN2CA11Transaction6commitEv + 568
20 QuartzCore 0x0000000114cfe51a _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76
21 CoreFoundation 0x0000000111717607 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
22 CoreFoundation 0x000000011171755e __CFRunLoopDoObservers + 430
23 CoreFoundation 0x00000001116fbb81 __CFRunLoopRun + 1537
24 CoreFoundation 0x00000001116fb30b CFRunLoopRunSpecific + 635
25 GraphicsServices 0x0000000113ecea73 GSEventRunModal + 62
26 UIKit 0x000000010e1e2057 UIApplicationMain + 159
27 _blackened_ 0x000000010d2896c7 main + 55
28 libdyld.dylib 0x00000001127b7955 start + 1
问候,
亚伦
最佳答案
问题解决了。
我认为问题在于调用collectionView(_:layout:sizeForItemAt:)
以确定collectionViewContentSize
属性。
但是我尝试访问其中的collectionViewContentSize
。
我使用bounds
的collectionView
修复了它。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let viewSize = collectionView.bounds.size
var cellWidth = viewSize.width / 2
if let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout {
let usableViewWidth = viewSize.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right
cellWidth = usableViewWidth / 2 - flowLayout.minimumInteritemSpacing / 2
}
return CGSize(width: cellWidth, height: cellWidth)
}