Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoration view not getting removed after deleting entire row in collection view

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 hereenter image description here

(Fig 1) Before deletion (Fig2) After deletion

like image 874
Anil Varghese Avatar asked Mar 07 '13 07:03

Anil Varghese


3 Answers

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];
    }

}
like image 105
Anil Varghese Avatar answered Sep 28 '22 00:09

Anil Varghese


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

like image 27
Jimmy Avatar answered Sep 28 '22 00:09

Jimmy


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.

like image 45
nolanw Avatar answered Sep 28 '22 00:09

nolanw