I have a UICollectionView. It scrolls horizontally, has only a single row of items, and behaves like a paging UIScrollView. I'm making something along the lines of the Safari tab picker, so you can still see the edge of each item. I only have one section.
If I delete an item that is not the last item, everything works as expected and a new item slides in from the right.
If I delete the last item, then the collection view's scroll position jumps to the N-1th item (doesn't smoothly animate), and then I see the Nth item (the one I deleted) fade out.
This behaviour isn't related to the custom layout I made, as it occurs even if I switch it to use a plain flow layout. I'm deleting the items using:
[self.tabCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
Has anyone else experienced this? Is it a bug in UICollectionView, and is there a workaround?
I was facing a similar issue with a single line horizontal scrolling through images collection view. The issue disappeared when i removed my code for setting the collection view's
contentSize
it seems to handle this automagically.Not a particularly verbose answer, but i hope it helps.
This will work:
And here's yet another solution (targetIndexPath is the indexPath of the cell to be removed). This code can simply be placed in a removeCellAtIndexPath:(NSIndexPath*)targetIndexPath method and you're done (assuming my adaptations from my code to public code are correct, otherwise ask me and I'll try to help).
The icn_cellAtIndexPathIsVisible: method is just a category on UICollectionView:
Update: This only works with one section.
I know this has an answer already but I implemented this in a slightly different way that doesn't require dispatching after a set interval.
In your delete method you would do a check to determine if the last item was being deleted. If it was call the following:
Assuming selection is the selected item you are deleting. This will scroll to the item to the left of it. Note the if statement checking that this is not the only item. If it were the call would crash as there is no -1 row.
Then you can implement the following method which is called when the scroll animation is complete. I simply set isDeleting to no in the deleteObjectInCollection method and it all seems to work.
I hope this helps.
A cheap solution is to add another 0px cell as the last cell and never remove it.
I managed to get my implementation working using the standard
UICollectionViewFlowLayout
. I had to create the animations manually.First, I caused the deleted cell to fade out using a basic animation:
Next, I caused the collection view to scroll to the previous cell. Once I've scrolled to the desired cell, I remove the deleted cell.
The only part that is slightly questionable is the
dispatch_after()
. Unfortunately,-scrollToItemAtIndexPath:atScrollPosition:animated:
does not have a completion block, so I had to simulate it. To avoid timing problems, I disabled user interaction. This prevents the user from interacting with the collection view before the cell is removed.Another thing I had to watch for is I have to reset my cell's alpha back to 1 due to cell reuse.
I hope this helps you with your Safari-style tab picker. I know your implementation is different from mine, and I hope that my solution works for you too.