访问 layout.collectionViewContentSize 时 collectionView(_:layout:sizeForItemAt:) 出现异常
Exception in collectionView(_:layout:sizeForItemAt:) when accessing layout.collectionViewContentSize
实现
我想在 UICollectionView
中有两种不同尺寸的商品。因此我实施了
collectionView(_:layout:sizeForItemAt:)
,共 UICollectionViewDelegateFlowLayout
。
错误
但我遇到的问题是,每当我在此函数中访问 collectionViewLayout.collectionViewContentSize
时,我的应用程序就会异常崩溃。
例外情况如下:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndexedSubscript:]: index 1 beyond bounds [0 .. 0]'.
Throw 调用堆栈位于 post 的底部。
看来是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
的对象分配给 UICollectionViewController
的 delegate
属性:
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
。
我使用 collectionView
的 bounds
修复了它。
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)
}
实现
我想在 UICollectionView
中有两种不同尺寸的商品。因此我实施了
collectionView(_:layout:sizeForItemAt:)
,共 UICollectionViewDelegateFlowLayout
。
错误
但我遇到的问题是,每当我在此函数中访问 collectionViewLayout.collectionViewContentSize
时,我的应用程序就会异常崩溃。
例外情况如下:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndexedSubscript:]: index 1 beyond bounds [0 .. 0]'.
Throw 调用堆栈位于 post 的底部。
看来是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
的对象分配给 UICollectionViewController
的 delegate
属性:
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
。
我使用 collectionView
的 bounds
修复了它。
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)
}