ios - UICollectionView not removing old cells after scroll -
i have uicollectionview grid of images. when tap on one, opens grid , shows subview details. this:
i open grid in uicollectionviewlayout adjusting uicollectionviewlayoutattributes , setting translation on transform3d property cells below current row of selected item. works nicely, , better animation , simpler approach first attempt @ inserting cell grid different size others.
anyway... works of time, after continued use see old images on collection view. ghost cells. can't click them, it's haven't been removed collection view properly, , sit on top of cells preventing taps , being nuisance. this:
any ideas why these cells doing this?
edit: i'd add, think happens when scroll collection view fast. i've written own uicollectionviewflowlayout replacement test if still happens. does.
edit 2: 3d transforms or layout have nothing this. must bug in uicollectionview. can exploit scrolling fast, letting come standstill , querying views on screen. there double number of cells, hidden stacked on top of each other. implementation above reveals them because of translation do.
this can hurt performance too.
see answer solution.
my second edit of question details why happenening, , here workaround. it's not bullet proof, works in case, , if experience somethign similar tweak solution:
- (void) removenaughtylingeringcells { // 1. find visible cells nsarray *visiblecells = self.collectionview.visiblecells; //nslog(@"we have %i visible cells", visiblecells.count); // 2. find visible rect of collection view on screen cgrect visiblerect; visiblerect.origin = self.collectionview.contentoffset; visiblerect.size = self.collectionview.bounds.size; //nslog(@"rect %@", nsstringfromcgrect(visiblerect)); // 3. find subviews shouldn't there , remove them //nslog(@"we have %i subviews", self.collectionview.subviews.count); (uiview *aview in [self.collectionview subviews]) { if ([aview iskindofclass:uicollectionviewcell.class]) { cgpoint origin = aview.frame.origin; if(cgrectcontainspoint(visiblerect, origin)) { if (![visiblecells containsobject:aview]) { [aview removefromsuperview]; } } } } //nslog(@"%i views shouldn't there", viewsshouldntbethere.count); // 4. refresh collection view display [self.collectionview setneedsdisplay]; }
and
- (void) scrollviewdidenddragging:(uiscrollview *)scrollview willdecelerate:(bool)decelerate { if (!decelerate) { [self removenaughtylingeringcells]; } } - (void) scrollviewdidenddecelerating:(uiscrollview *)scrollview { [self removenaughtylingeringcells]; }
Comments
Post a Comment