ios - Removing the last Cell in UICollectionView makes a Crash -
hi i'm working custom uicollectionview (https://github.com/surecase/waterfallcollectionview) works fine. i'm setting delete items uicollectionview, , can delete them fine. problem comes when i'm trying delete last item of section.
it gives following error.
*** assertion failure in -[uicollectionviewdata layoutattributesforsupplementaryelementofkind:atindexpath:], /sourcecache/uikit/uikit-2935.137/uicollectionviewdata.m:787 *** terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'no uicollectionviewlayoutattributes instance -layoutattributesforsupplementaryelementofkind: uicollectionelementkindsectionheader @ path <nsindexpath: 0xc000000000000016> {length = 2, path = 0 - 0}'
the code i'm using delete items following:
- (void) alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex { if (buttonindex == 0){ nslog(@"erasing objects!"); //first remove items datasource, , after collection view [self deleteitemsfromdatasourceatindexpaths:selectedindexes]; [self.cv deleteitemsatindexpaths:selectedindexes]; [selectediobjectsindexes removeallobjects]; [selectedindexes removeallobjects]; edituicollection=no; easymediagreenview.hidden = yes; dispatch_after(dispatch_time(dispatch_time_now, 0.7 * nsec_per_sec), dispatch_get_main_queue(), ^{ [self.cv reloaddata]; }); }else{ nslog(@"not delete"); } }
so first i'm removing items datasource , de collection view. here code removing data source.
-(void)deleteitemsfromdatasourceatindexpaths:(nsarray *)itempaths { nsmutableindexset *indexset = [nsmutableindexset indexset]; (nsindexpath *itempath in itempaths) { [indexset addindex:itempath.row]; } [idobject removeobjectsatindexes:indexset]; [typeobject removeobjectsatindexes:indexset]; [urlobject removeobjectsatindexes:indexset]; [textobject removeobjectsatindexes:indexset]; }
why happening when try remove last object, when other objects removed correctly? understand part, & idea how fix this? all.
i found solution! returning 1 section, no items collectionview being constructed, , asking build header, this:
- (uicollectionreusableview *)collectionview:(uicollectionview *)collectionview viewforsupplementaryelementofkind:(nsstring *)kind atindexpath:(nsindexpath *)indexpath; {
so counted items in data array, , if array empty, there shouldn't section.
- (nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview { if(idobject.count>0){ return 1; } else{ return 0; } }
so i'm deleting items data array, can check if array empty, if not empty i'll delete items, if is, i'll delete entire section.
nslog(@"erasing objects!"); [self deleteitemsfromdatasourceatindexpaths:selectedindexes]; if(idshadeinside.count > 0){ [self.cv deleteitemsatindexpaths:selectedindexes]; } else{ //creating indexset section erase nsmutableindexset *indexset = [nsmutableindexset indexset];[indexset addindex:0]; [self.cv deletesections:indexset]; }
so problem fixed!
Comments
Post a Comment