UICollectionView sizeForItemAt IndexPath

2019-08-02 11:14发布

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)



}

}

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)

1条回答
beautiful°
2楼-- · 2019-08-02 12:04

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.

查看更多
登录 后发表回答