lxcid / LXReorderableCollectionViewFlowLayout

Extends `UICollectionViewFlowLayout` to support reordering of cells. Similar to long press and pan on books in iBook.
http://lxcid.com/
MIT License
1.86k stars 328 forks source link

cellForItemAtIndexPath: returns nil for reordered cell #8

Closed simonbs closed 11 years ago

simonbs commented 11 years ago

I apply an animation to cells when they can be moved. Much like Apple does when reordering apps on the springboard. The animation disappears after a cell has been moved. This is not a problem, I can just apply it again afterwards but it's not possible to grab the cell again as cellForItemAtIndexPath: will return null for the index path which the cell was moved to.

Example:

- (void)collectionView:(PSTCollectionView *)theCollectionView layout:(PSTCollectionViewLayout *)theLayout didEndReorderingAtIndexPath:(NSIndexPath *)theIndexPath
{
    PSTCollectionViewCell *cell = [theCollectionView cellForItemAtIndexPath:theIndexPath]; // is null
}
lxcid commented 11 years ago

It seems that while animating (in animation block and completion block), querying of the index path of cell result in the collection view returning nil. I haven't read this behavior in the documentation so it seems a surprise for me.

Luckily, we could dispatch the query immediately after with the following code.

- (void)collectionView:(UICollectionView *)theCollectionView layout:(UICollectionViewLayout *)theLayout didEndReorderingAtIndexPath:(NSIndexPath *)theIndexPath {
    double delayInSeconds = 0.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        UICollectionViewCell *cell = [theCollectionView cellForItemAtIndexPath:theIndexPath]; // cell available
        NSLog(@"%@", cell);
    });
}