gpt4 book ai didi

ios - collectionView 中的异常(_ :layout:sizeForItemAt:) when accessing layout. collectionViewContentSize

转载 作者:行者123 更新时间:2023-11-30 11:20:56 26 4
gpt4 key购买 nike

实现

我想在 UICollectionView 中拥有两种不同的项目大小。因此我已经实现了

UICollectionViewDelegateFlowLayoutcollectionView(_:layout:sizeForItemAt:)

错误

但我遇到的问题是,每当我在此函数中访问 collectionViewLayout.collectionViewContentSize 时,我的应用程序都会崩溃并出现异常。

异常(exception)情况如下:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndexedSubscript:]: index 1 beyond bounds [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)
}
}

在 Collection View Controller 子类中,我将 TwoSizesCellFlowLayout 对象分配给 UICollectionViewControllerdelegate 属性:

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

我使用collectionViewbounds修复了它。

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)
}

关于ios - collectionView 中的异常(_ :layout:sizeForItemAt:) when accessing layout. collectionViewContentSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51279230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com