gpt4 book ai didi

swift - UICollectionView sizeForItemAt IndexPath

转载 作者:行者123 更新时间:2023-11-28 06:19:52 30 4
gpt4 key购买 nike

我正在尝试根据里面的内容设置我的单元格的大小这是我的代码,它崩溃了,我找不到错误消息。

extension TimelineCollectionVC: UICollectionViewDelegateFlowLayout {



func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeline", for: indexPath) as? Timeline

cell?.textView.translatesAutoresizingMaskIntoConstraints = true
cell?.textView.sizeToFit()



if cell?.containerView.subviews.count == 0 {
cell?.containerHeight.constant = 1
} else {
cell?.containerHeight.constant = (cell?.containerView.subviews.first?.frame.size.height) ?? 80
}

let cellHeight = (cell?.upperView.bounds.height)! + (cell?.textView.bounds.height)! + (cell?.containerView.bounds.height)! + (cell?.lowerView.bounds.height)!


return CGSize(width: 375, height: cellHeight)



}

原因:'-[NSCFString setSizeHasBeenSet:]: 无法识别的选择器发送到实例 0x610000076bc0'*** 首先抛出调用栈:( 0 CoreFoundation 0x0000000109dd9d4b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x00000001094d921e objc_exception_throw + 48 2 CoreFoundation 0x0000000109e49f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x0000000109d5f005 ___forwarding_ + 1013 4 核心基础 0x0000000109d5eb88 _CF_forwarding_prep_0 + 120 5 UIKit 0x00000001080d9485 -[UICollectionViewFlowLayout _getSizingInfosWithExistingSizingDictionary:] + 3691 6 UIKit 0x00000001080da97b -[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 127 7 UIKit 0x00000001080d3504 -[UICollectionViewFlowLayout prepareLayout] + 273 8 UIKit 0x00000001080f3d6c -[UICollectionViewData _prepareToLoadData] + 159 9 UIKit 0x00000001080f4618 -[UICollectionViewData validateLayoutInRect:] + 57 10 UIKit 0x000000010809b6d4 -[UICollectionView layoutSubviews] + 232 11 UIKit 0x0000000107817ab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237 12 QuartzCore 0x0000000106fcdbf8 -[CALayer layoutSublayers] + 146 13 quartz 核心 0x0000000106fc1440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 14 QuartzCore 0x0000000106fc12be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 15 quartz 核 0x0000000106f4f318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280 16 quartz 核心 0x0000000106f7c3ff _ZN2CA11Transaction6commitEv + 475 17 quartz 核 0x0000000106f7cd6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113 18 核心基础 0x0000000109d7e267 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 19 核心基础 0x0000000109d7e1d7 __CFRunLoopDoObservers + 391 20 CoreFoundation 0x0000000109d628a6 CFRunLoopRunSpecific + 454 21 UIKit 0x000000010774caea -[UIApplication_run] + 434 22 UIKit 0x0000000107752c68 UIApplicationMain + 159 23 摩登 0x0000000104ab4fbf 主 + 111 24 libdyld.dylib 0x000000010ad4e68d 开始 + 1)libc++abi.dylib:以 NSException 类型的未捕获异常终止(lldb)

最佳答案

你不应该在这里使用 dequeueReusableCell。这仅用于创建新单元格。在这种情况下,您想要获取一个现有的单元格,并且可能应该在 UICollectionView 类中使用 func cellForItem(at indexPath: IndexPath) -> UICollectionViewCell?

关于swift - UICollectionView sizeForItemAt IndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43955143/

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