2

In other words -- how do you know, inside a UICollectionViewFlowLayout, when the associated collection view has had the data reloaded?


I'm using the popular and awesome BPXLFlowLayout by Brandon Alexander,

https://github.com/whilethis/UIKit-Dynamics-101/blob/master/Collection%20View%20Dynamics/Collection%20View%20Dynamics/BPXLFlowLayout.m

This is the best way to make "bouncy" collections, such as the iOS Messages app.

How to replicate Messages bouncing bubbles in iOS 7

I found a real problem in BPXLFlowLayout and other UICollectionViewFlowLayout approaches. Typically, you only "use them once" with the one layout, the one collection view. Say you have a collection view "BouncyViewHouses"

@interface BouncyViewHouses : UICollectionViewController

which uses BPXLFlowLayout

Everything's great until you reload. Or confusingly to me, if you use the same BPXLFlowLayout on ANOTHER collection view, you seem to have the same problem.

To fix the problem, you have to do something like this

@implementation BouncyViewHouses

-(void)safelyReloadBouncyTable
    {
    [self.collectionView reloadData];
    [(BPXLFlowLayout *)self.collectionView.collectionViewLayout fastSignal];
    }

Whenever you reload the collection, you have to "MANUALLY" zero out the animator.

So for example inside BPXLFlowLayout I added a new method ...

@interface BPXLFlowLayout ()
@property (nonatomic, strong) UIDynamicAnimator *animator;
@end

@implementation BPXLFlowLayout

-(void)fastSignal
    {
    // it's very difficult to have prepareLayout (or, anything) called when you
    // reload the collection data. in fact, you must call this manually.
    self.animator = nil;
    }

and I have to call that MANUALLY from collection views, when I reload the data source.

(If you don't do this, you'll get really hairy problems that are hard to figure out!)

My problem .. you'd think that inside BPXLFlowLayout (or similar) you could just call self.animator = nil;

inside -(void)prepareLayout or perhaps shouldInvalidateLayoutForBoundsChange:

but it doesn't work! I tried everything, in a word I can't figure out "what is triggered" inside a UICollectionViewFlowLayout, when the data source is reloaded on the associated collection view.

I guess it's possible, NOTHING is really triggered in a UICollectionViewFlowLayout .. in that case, my approach, of adding a manual tickle which you "must call" when you reload data, I guess that's the only way.

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Thanks this seemed to have fixed a problem I was having where new cells would disappear after scrolling – Landon Jul 01 '14 at 16:17
  • Well it did fix the disappearing cells problem but it also seems to have gotten rid of the dynamic behaviors applied by the BPXL class... – Landon Jul 01 '14 at 16:31
  • Hi Landon -- I'd love to work with you on that, as the whole thing is a bit of a mystery. I think you are I are at the point of this problem. I think I was experiencing **exactly the new problem you describe in your second comment** but we fixed it somehow. Question, do you have **just the one bouncy view** or do you have **a number of bouncy views**. I mean that whether "appearing at the same time" or "only on different scenes..." Cheers – Fattie Jul 02 '14 at 08:21
  • Hi Joe - After I had the problem of disappearing cells in my project, I tried to simulate my desired effect in the Collection View Dynamics BPXL sample project. The only changes I made to this was in the view controller. I added a mutable array with a few objects in viewdidload and called [self performSelector:@selector(moreObjects) withObject:self afterDelay:2.0] to then add another object to the array. Everything looks good until the second I try to scroll. At that point, the new object in the array disappears. – Landon Jul 03 '14 at 15:43
  • When I added self.animator = nil at first i thought this fixed the problem and it did in fact fix the problem of the disappearing new cell but it also completely got rid of the behaviors applied to the cells – Landon Jul 03 '14 at 15:47
  • I have been looking at the ASHSpringlyCollectionViewFlowLayout which has a similar effect but doesn't have the problems we seem to be experiencing and seeing if I can find a fix – Landon Jul 03 '14 at 15:48
  • I will try to have a go with https://github.com/AshFurrow/ASHSpringyCollectionView/blob/master/ASHSpringyCollectionView/ASHSpringyCollectionViewFlowLayout.m when I get a minute... – Fattie Jul 04 '14 at 08:04
  • "When I added self.animator = nil" ... in fact where did you add it? And indeed, "how" do you call that? I have to call it explicitly from another class. Perhaps click an "answer" here and you can paste in some code you know.. – Fattie Jul 04 '14 at 08:05
  • "At that point, the new object in the array disappears" .. notice in my example code how I have to call the "fastSignal" each time I reload the table. Are you doing that or something similar ?? – Fattie Jul 04 '14 at 08:05
  • the new item is no longer disappearing when I call fastSignal. Not entirely sure what I changed... must have been the placement of fastSignal – Landon Jul 07 '14 at 03:06

0 Answers0