Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deleting cells with variable heights #10

Open
wcrtr opened this issue Mar 15, 2014 · 3 comments
Open

Deleting cells with variable heights #10

wcrtr opened this issue Mar 15, 2014 · 3 comments

Comments

@wcrtr
Copy link

wcrtr commented Mar 15, 2014

Hey,

Great demo / project!

I've been experimenting with using the TLSpringFlowLayout with a UICollectionView with dynamically sized items, and everything works like a charm except deleting.

Basically what happens is I use a method to calculate the height of a cell based on it's content, which I do normally in the datasource here:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGRect estimatedRect = [height calculated from cell content]
    return CGSizeMake(300, estimatedRect.size.height);

}

All is fine and good until I try and delete one of these items with variable height. What happens is the dynamic animator returns the incorrect attributes here:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    return [self.dynamicAnimator itemsInRect:rect];
}

The layout attributes returned are the attributes of the deleted cell that was "above" the cell, not what should be retained by the cell itself. If I comment out the above code block, the attributes are retained correctly.

To say it another way, sizeForItemAtIndexPath is returning the correct size, but layoutAttributesForElementsInRect from the dynamic animator is returning the size from a deleted item.

Any ideas here? It's possible this is not a problem with this class itself, I'm just struggling to find answers and have isolated it here. I've also done a bunch of research into the issue and can't find much in way of answers so I thought it might be productive to start a thread here.

Thanks again for any insight!

@ashfurrow
Copy link
Contributor

Tricky tricky. Deleting cells, in general, can be an exercise in frustration. I had to do some hacky things to get it to work for me, and that was without variable cell size! I honestly don't know where you'd start on this one – you've already covered the basics. Hmmm. I'll give it some more thought and see what I can think of. If all else fails, you can just reload the collection view when deletions occur.

Best of luck!
Ash

@wcrtr
Copy link
Author

wcrtr commented Mar 15, 2014

I kinda got it working in a hacky way by appending the following to the prepareForCollectionViewUpdates method

        } else if(updateItem.updateAction == UICollectionUpdateActionDelete) {
            if([self.dynamicAnimator layoutAttributesForCellAtIndexPath:updateItem.indexPathAfterUpdate])
            {
                return;
            }

            [self.dynamicAnimator removeAllBehaviors];
            self.visibleIndexPathsSet = [NSMutableSet set];

            UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:updateItem.indexPathAfterUpdate];
        }

There are some limitations to this approach, one being that it seems like cells that weren't visible at the time of deletion get their heights set incorrectly still... but certainly closer.

It seems like if there were a way to delete the dynamic behaviors given known deleted cells, it might work without too much bother, but there doesn't seem to be a great way to do that save maybe keeping a set of all the dynamic behaviors and trying to figure out, at time of deletion, which ones to delete.

I'll keep tracking progress here if I make any...

@atomkirk
Copy link

atomkirk commented May 7, 2014

See if my additions help: #12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants