ios - 'No UICollectionViewLayoutAttributes instance for -layoutAttributesForItemAtIndexPath' error using custom UICollectionViewLayout -
i'm using custom uicollectionviewlayout :
override func preparelayout() { cache.removeall(keepcapacity: false) let standardheight = layoutconstants.cell.standardheight let featuredheight = layoutconstants.cell.featuredheight var frame = cgrectzero var y: cgfloat = 0 item in 0..<numberofitems { let indexpath = nsindexpath(foritem: item, insection: 0) let attributes = uicollectionviewlayoutattributes(forcellwithindexpath: indexpath) let cell = collectionview!.cellforitematindexpath(indexpath) /* important because each cell has slide on top of previous 1 */ attributes.zindex = item /* set height of cell standard height */ var height = standardheight if indexpath.item == featureditemindex { /* featured cell */ let yoffset = standardheight * nextitempercentageoffset y = collectionview!.contentoffset.y - yoffset height = featuredheight } else if indexpath.item == (featureditemindex + 1) && indexpath.item != numberofitems { /* cell directly below featured cell, grows user scrolls */ let maxy = y + standardheight height = standardheight + max((featuredheight - standardheight) * nextitempercentageoffset, 0) y = maxy - height } frame = cgrect(x: 0, y: y, width: width, height: height) attributes.frame = frame cache.append(attributes) y = cgrectgetmaxy(frame) } } /* return attributes in cache frame intersects rect passed method */ override func layoutattributesforelementsinrect(rect: cgrect) -> [anyobject]? { var layoutattributes = [uicollectionviewlayoutattributes]() attributes in cache { if cgrectintersectsrect(attributes.frame, rect) { layoutattributes.append(attributes) } } return layoutattributes }
that's works want if wanna insert other item, app crash error :
2015-05-31 22:37:29.032 flore-jessy[15359:717220] *** assertion failure in -[uicollectionviewdata layoutattributesforitematindexpath:], /sourcecache/uikit_sim/uikit-3347.44/uicollectionviewdata.m:694 2015-05-31 22:37:29.077 flore-jessy[15359:717220] *** terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'no uicollectionviewlayoutattributes instance -layoutattributesforitematindexpath: <nsindexpath: 0x7b952a70> {length = 2, path = 0 - 15}' *** first throw call stack: ( 0 corefoundation 0x00a0a746 __exceptionpreprocess + 182 1 libobjc.a.dylib 0x02867a97 objc_exception_throw + 44 2 corefoundation 0x00a0a5da +[nsexception raise:format:arguments:] + 138 3 foundation 0x01093720 -[nsassertionhandler handlefailureinmethod:object:file:linenumber:description:] + 118 4 uikit 0x01de5c74 -[uicollectionviewdata layoutattributesforitematindexpath:] + 461 5 uikit 0x01dafbe7 __51-[uicollectionview _viewanimationsforcurrentupdate]_block_invoke1382 + 161 6 uikit 0x01dad73b -[uicollectionview _viewanimationsforcurrentupdate] + 4789 7 uikit 0x01db0eb8 -[uicollectionview _updatewithitems:tentativelyforreordering:] + 2447 8 uikit 0x01dabf0d -[uicollectionview _enditemanimationswithinvalidationcontext:tentativelyforreordering:] + 14802 9 uikit 0x01da8536 -[uicollectionview _enditemanimationswithinvalidationcontext:] + 48 10 uikit 0x01da82ed -[uicollectionview _updaterowsatindexpaths:updateaction:] + 374 11 uikit 0x01da833f -[uicollectionview insertitemsatindexpaths:] + 48 12 flore-jessy 0x0009183c _tfc11flore_jessy22timelineviewcontroller13viewdidappearfs0_fsbt_ + 556 13 flore-jessy 0x000918cf _ttofc11flore_jessy22timelineviewcontroller13viewdidappearfs0_fsbt_ + 63 14 uikit 0x017f3245 -[uiviewcontroller _setviewappearstate:isanimating:] + 629 15 uikit 0x017f3860 -[uiviewcontroller __viewdidappear:] + 171 16 uikit 0x017f3b2b -[uiviewcontroller _endappearancetransition:] + 322 17 uikit 0x0180bd4e -[uiviewcontroller(uicontainerviewcontrollerprotectedmethods) endappearancetransition] + 41 18 uikit 0x017c403b -[uipresentationcontroller transitiondidfinish:] + 839 19 uikit 0x01e50109 -[_uifullscreenpresentationcontroller transitiondidfinish:] + 59 20 uikit 0x017c6c11 __56-[uipresentationcontroller runtransitionforcurrentstate]_block_invoke_2 + 165 21 uikit 0x01ef444a -[_uiviewcontrollertransitioncontext completetransition:] + 124 22 flore-jessy 0x0008e174 _tffc11flore_jessy29swipebluredtransitionanimator17animatetransitionfs0_fpso36uiviewcontrollercontexttransitioning_t_u0_fsbt_ + 436 23 flore-jessy 0x0008ca34 _tpa__tffc11flore_jessy29swipebluredtransitionanimator17animatetransitionfs0_fpso36uiviewcontrollercontexttransitioning_t_u0_fsbt_ + 68 24 flore-jessy 0x0008e1bc _ttrxfo_dsb_dt__xfo_isb_it__ + 28 25 flore-jessy 0x0008caea _tpa__ttrxfo_dsb_dt__xfo_isb_it__ + 90 26 flore-jessy 0x0008e1f7 _ttrxfo_isb_it__xfo_dsb_dt__ + 39 27 flore-jessy 0x0008cb9e _tpa__ttrxfo_isb_it__xfo_dsb_dt__ + 78 28 flore-jessy 0x0008e245 _ttrxfo_dsb_dt__xfdcb_dv10objectivec8objcbool_dt__ + 69 29 uikit 0x017149be -[uiviewanimationblockdelegate _didendblockanimation:finished:context:] + 318 30 uikit 0x016f81b2 -[uiviewanimationstate senddelegateanimationdidstop:finished:] + 206 31 uikit 0x016f8522 -[uiviewanimationstate animationdidstop:finished:] + 80 32 quartzcore 0x0154a571 _zn2ca5layer23run_animation_callbacksepv + 307 33 libdispatch.dylib 0x02f59bef _dispatch_client_callout + 14 34 libdispatch.dylib 0x02f3f6bb _dispatch_main_queue_callback_4cf + 993 35 corefoundation 0x009638ee __cfrunloop_is_servicing_the_main_dispatch_queue__ + 14 36 corefoundation 0x009215f0 __cfrunlooprun + 2256 37 corefoundation 0x00920a5b cfrunlooprunspecific + 443 38 corefoundation 0x0092088b cfrunloopruninmode + 123 39 graphicsservices 0x051e02c9 gseventrunmodal + 192 40 graphicsservices 0x051e0106 gseventrun + 104 41 uikit 0x0168a106 uiapplicationmain + 1526 42 flore-jessy 0x00080a24 main + 180 43 libdyld.dylib 0x02f84ac9 start + 1 ) libc++abi.dylib: terminating uncaught exception of type nsexception
this code use insert new item
override func viewdidappear(animated: bool) { super.viewdidappear(animated) if let post = newpost { collectionview!.insertitemsatindexpaths([datasource.indexpathfornewpost(post)]) } } /// return new indexpath timelineitem func indexpathfornewpost(item: timelineitem) -> nsindexpath { if let liste = listposts { listposts!.append(item) } return indexpathforpost(item) } func indexpathforpost(item: timelineitem) -> nsindexpath { var itemindex = 0 (index, currenttimelineitem) in enumerate(allposts()) { if currenttimelineitem === item { itemindex = index break } } return nsindexpath(foritem: itemindex, insection: 0) }
any idea ?
base on error message, because of cell can't retrieve layoutattributes. layoutattributesforitematindexpath
returns nil if cell created update not visible.
to fix error, override layoutattributesforitematindexpath
method:
override func layoutattributesforitematindexpath(indexpath: nsindexpath) -> uicollectionviewlayoutattributes? { return cache[indexpath.row] }
swift 3 -
override func layoutattributesforitem(at indexpath: indexpath) -> uicollectionviewlayoutattributes? { return cache[indexpath.row] }
Comments
Post a Comment