UICollectionView sizeForItemAt IndexPath [英] UICollectionView sizeForItemAt IndexPath

查看:63
本文介绍了UICollectionView sizeForItemAt IndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据
中的内容设置单元格的大小,这是我的代码,它崩溃了,我找不到错误信息。

I'm trying to set size for my cell according to what's inside this is my code and it crashes and I can't find an error messege.

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)dosNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000109d5f005 ___ forwarding _
+ 1013
4 CoreFoundation 0x0000000109d5eb88 _CF_forwarding_prep_Single_WithPingCollection 0-120db
$ b948b ] + 3691
6 UIKit 0x 00000001080da97b-[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 127
7 UIKit 0x00000001080d3504-[UICollectionViewFlowLayout prepareLayout] + 273
8 UIKit 0x00000001080f3d6c-[UICollectionViewData _prepareToLoadData] + 159
9In [0] + 57
10 UIKit 0x000000010809b6d4-[UICollectionView layoutSubviews] + 232
11 UIKit 0x0000000107817ab8-[UIView(CALayerDelegate)layoutSublayersOfLayer:] + 1237
12 QuartzCore 0x0000000106fcdbf8-[CALayer layoutSublayers] $ b 13 QuartzCore 0x0000000106fc1440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
14 QuartzCore 0x0000000106fc12be _ZN2CA5Layer28layout_and_display_if_neede dEPNS_11TransactionE + 24
15分配QuartzCore 0x0000000106f4f318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
16分配QuartzCore 0x0000000106f7c3ff _ZN2CA11Transaction6commitEv + 475
17分配QuartzCore 0x0000000106f7cd6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
18分配的CoreFoundation 0x0000000109d7e267的 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23
19 CoreFoundation 0x0000000109d7e1d7 __CFRunLoopDoObservers + 391
20 CoreFoundation 0x0000000109d628a6 CFRunLoopRunSpecific + 454
21 UIKit 0x000000010774caea-[UIApplication _run] + 434
22 UIKit 0x0000000102 0x0000000104ab4fbf主+ 1 11
24 libdyld.dylib 0x000000010ad4e68d start + 1

libc ++ abi.dylib:以类型为NSException的未捕获异常终止
(lldb)

reason: '-[NSCFString setSizeHasBeenSet:]: unrecognized selector sent to instance 0x610000076bc0' *** First throw call stack: ( 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 CoreFoundation 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 QuartzCore 0x0000000106fc1440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 14 QuartzCore 0x0000000106fc12be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 15 QuartzCore 0x0000000106f4f318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280 16 QuartzCore 0x0000000106f7c3ff _ZN2CA11Transaction6commitEv + 475 17 QuartzCore 0x0000000106f7cd6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113 18 CoreFoundation 0x0000000109d7e267 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 19 CoreFoundation 0x0000000109d7e1d7 __CFRunLoopDoObservers + 391 20 CoreFoundation 0x0000000109d628a6 CFRunLoopRunSpecific + 454 21 UIKit 0x000000010774caea -[UIApplication _run] + 434 22 UIKit 0x0000000107752c68 UIApplicationMain + 159 23 Moden 0x0000000104ab4fbf main + 111 24 libdyld.dylib 0x000000010ad4e68d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

推荐答案

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

You should not be using dequeueReusableCell here. That is only for creating new cells. In this case you want to get an existing cell and should probably be using func cellForItem(at indexPath: IndexPath) -> UICollectionViewCell? in the UICollectionView class.

这篇关于UICollectionView sizeForItemAt IndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆