UICollectionView sizeForItemAt IndexPath

时间:2017-05-13 15:58:48

标签: swift uicollectionview size cell

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

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 ___转发_ + 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:以NSException类型的未捕获异常终止 (lldb)

1 个答案:

答案 0 :(得分:1)

你不应该在这里使用dequeueReusableCell。那只是为了创造新的细胞。在这种情况下,您希望获得一个现有单元格,并且应该在func cellForItem(at indexPath: IndexPath) -> UICollectionViewCell?类中使用UICollectionView