UICollectionView sizeForItemAt IndexPath

ฉันกำลังพยายามกำหนดขนาดเซลล์ของฉันตามสิ่งที่อยู่ภายใน นี่คือโค้ดของฉัน และมันขัดข้อง และฉันไม่พบข้อความแสดงข้อผิดพลาด

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)



}

}

คุณไม่ควรใช้

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)



}
ที่นี่ นั่นเป็นเพียงการสร้างเซลล์ใหม่เท่านั้น ในกรณีนี้ คุณต้องการรับเซลล์ที่มีอยู่ และน่าจะใช้ _2_ ในคลาส _3_


person Mohammed Asheeba    schedule 13.05.2017    source แหล่งที่มา
comment
ใช้ดีบักเกอร์และทำตามขั้นตอนโค้ด BTW - ไม่มีเหตุผลที่จะสร้าง _1_ และเป็นทางเลือก หากไม่ใช่อินสแตนซ์ _2_ จริงๆ คุณอยากให้มันหยุดทำงานเนื่องจากนั่นเป็นข้อบกพร่องที่ชัดเจนในแอปของคุณ จากนั้นคุณก็สามารถกำจัด _3_ ที่ไม่จำเป็นออกไปทั้งหมดในการอ้างอิง _4_ ทั้งหมดได้ จากนั้นคุณก็สามารถกำจัดตัวดำเนินการ _5_ ที่น่ารังเกียจเหล่านั้นได้ (ซึ่งหมายถึงความผิดพลาดที่นี่)   -  person Scott Thompson    schedule 13.05.2017
comment
@ScottThompson ฉันไม่ได้รับบันทึกข้อขัดข้อง มันแค่ขัดข้องและแสดงคลาส AppDelegate ของฉัน (Thread1: EXC_BAD_ACCESS~)   -  person rmaddy    schedule 13.05.2017
comment
@rmaddy ลบเครื่องหมายที่ยังคงหยุดทำงานหากฉันส่งคืน CGSize(width: 375, height: 600) โดยไม่มีสิ่งใดอยู่ด้านบนเลย มันทำงานได้ดี แต่เมื่อเซลล์เข้าไปพัวพันก็จะเกิดการขัดข้องขึ้นและไม่มีเหตุผลใดปรากฏให้เห็น   -  person Mohammed Asheeba    schedule 13.05.2017
comment
ฉันประสบปัญหาที่คล้ายกัน แต่เมื่อฉันใช้ข้อความประกอบในเซลล์ของฉันเท่านั้น คุณควรใช้ la sizingView = Bundle.main.loadNibNamed(MyNib, Owner: self, options: nil)?[0] as? มายเซลล์   -  person Mohammed Asheeba    schedule 13.05.2017
comment
ให้ sizingView = Bundle.main.loadNibNamed (MyNib, เจ้าของ: ตัวเอง, ตัวเลือก: ไม่มี)? [0] เป็น? MyCell แต่นี้ไม่ได้ relove ปัญหา   -  person Ryan Heitner    schedule 31.07.2017


คำตอบ (1)


คุณสามารถโพสต์บันทึกข้อขัดข้องหรืออย่างน้อยก็ที่ด้านบนของบันทึกข้อขัดข้องได้หรือไม่

person Scott Thompson    schedule 13.05.2017
comment
เหตุผล: '-[NSCFString setSizeHasBeenSet:]: ตัวเลือกที่ไม่รู้จักส่งไปยังอินสแตนซ์ 0x610000076bc0' *** สแต็กการเรียกครั้งแรก: ( 0 CoreFoundation 0x0000000109dd9d4b __ExceptionPreprocess + 171 1 libobjc.A.dylib 0x00000001094d921e obj c_Exception_throw + 48 2 รากฐานหลัก 0x0000000109e49f04 - [ NSObject (NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x0000000109d5f005 ___ การส่งต่อ_ + 1,013 4 CoreFoundation 0x0000000109d5eb88 _CF_forwarding_prep_0 + 120 5 UIKit 0x00000001 080d9485 - [UICollectionViewFlowLayout _getSizingInfosWithExistingSizingDictionary:] + 3691 6 UIKit 0x00000001080da97b - [UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 127 7 UIKit 0x00000001080d3504 - [UICollectionViewFlowLayout waitLayout] + 273 8 UIKit 0x00000001080f3d6c - [UICollectionViewData _prepareToLoadData] + 159 9 UIKit 0x00000001080f4618 - [UICollectionViewData validateLayoutInRect:] + 57 10 UIKit 0x000 000010809b6d4 - [UICollectionView เค้าโครง Subviews] + 232 11 UIKit 0x0000000107817ab8 - [UIView (CALayerDelegate) เค้าโครง SublayersOfLayer:] + 1237 12 QuartzCore 0x0000000106fcdbf8 - [CALayer รูปแบบ Sublayers] + 146 13 QuartzCore 0x0000000106fc1440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 14 QuartzCore 0x0000000106fc12be _Z N2CA5Layer28layout_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 หลัก + 111 24 libdyld.dylib 0x000000010ad4e68d เริ่มต้น + 1 ) libc+ +abi.dylib: สิ้นสุดด้วยข้อยกเว้นที่ไม่ถูกตรวจจับประเภท NSException (lldb) - person Ryan Heitner; 31.07.2017