6

I have added decoration view to the UICollectionView by sub classing UICollectionViewFlowLayout. Im placing decoration view under each row in collection view. Its works fine. Decoration views appearing. But the problem is decoration views are not getting removed from the collection view after deleting one entire row of items. But the the header and footer views are repositioned correctly it's not handled by me. I dont know where to remove decoration view after deletion. help me. My calculation of decoration view in prepareLayout is fine there number of decoration views and frame is correct

enter image description here enter image description here

(Fig 1) Before deletion (Fig2) After deletion

4 Answers 4

3

I didn't get an answer from any of the other sources. So iam going to answer from my experience. Actually the collection view wont remove the decoration view's even supplementary views(Header/footer) after deleting the items. That you have to do manually. Probably it will be a bug in collectionView.

Remove the decoration views in the prepareLayout method

 /// Collection view is not removing the added decoraion views afeter deletion. Remove yourself to fix that
for (UIView *view in self.collectionView.subviews) {
    if ([view isKindOfClass:[DecorationView class]])
    {
        [view removeFromSuperview];
    }

}
2

Use methods in UICollectionViewLayout to remove supplementary/decoration View:

func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem])

func indexPathsToDeleteForSupplementaryView(ofKind elementKind: String) -> [IndexPath]

func indexPathsToInsertForDecorationView(ofKind elementKind: String) -> [IndexPath]

if you're not familiar with them, well, you should read the document really carefully

1

As of iOS 7 you can override -indexPathsToInsertForDecorationViewOfKind: and -indexPathsToDeleteForDecorationViewOfKind: in your custom layout to add/remove decoration views whenever the collection view data changes.

0

This actually is not a bug. Decoration views per the docs are independent of the datasource.

Decoration views are visual adornments that enhance the appearance of your collection view layouts. Unlike cells and supplementary views, decoration views provide visual content only and are thus independent of the data source. You can use them to provide custom backgrounds, fill in the spaces around cells, or even obscure cells if you want. Decoration views are defined and managed solely by the layout object and do not interact with the collection view’s data source object.

A decoration view would be better suited if you wanted to say, add a background image behind your collection view, its display remaining independent of the data.

Headers, footers, and other supplementary views can be updated using the data source via the method:

  collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView

And their positioning and layout can be controlled by overriding

layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? 

And

layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! 

From within your custom flow layout.

It looks to me like the best way to solve the issue, if you did not want to manually find and remove your decoration views, would be to replace your decoration view with a supplementary view, that way it can be updated using the data source.

1
  • Do you have any example of this Jul 28, 2016 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.