在集合视图中删除整行后,装饰视图未被删除

时间:2013-03-07 07:00:54

标签: ios uicollectionview

我已通过子类UICollectionViewUICollectionViewFlowLayout添加了装饰视图。我m placing decoration view under each row in collection view. It的工作正常。装饰视图出现。但问题是在删除整行项目后,装饰视图不会从集合视图中删除。但页眉和页脚视图正确地重新定位它不是我处理的。删除后我不知道删除装饰视图的位置。帮我。我在prepareLayout中对装饰视图的计算很好,装饰视图的数量和框架是正确的

enter image description here enter image description here

(图1)删除前(图2)删除后

4 个答案:

答案 0 :(得分:2)

我没有得到任何其他来源的回答。所以我会从我的经验中回答。实际上, 集合视图在删除项目 后不会删除装饰视图甚至补充视图(页眉/页脚)。你必须手动完成。可能它将是collectionView中的一个错误。

删除 prepareLayout 方法中的装饰视图

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

}

答案 1 :(得分:2)

使用UICollectionViewLayout中的方法删除补充/装饰视图:

func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem])

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

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

如果你不熟悉它们,那么你应该仔细阅读document

答案 2 :(得分:1)

从iOS 7开始,您可以在自定义布局中覆盖-indexPathsToInsertForDecorationViewOfKind:-indexPathsToDeleteForDecorationViewOfKind:,以便在集合视图数据发生更改时添加/删除装饰视图。

答案 3 :(得分:0)

这实际上不是一个错误。装饰视图per the docs与数据源无关。

  

装饰视图是可视化装饰,可增强集合视图布局的外观。与单元格和补充视图不同,装饰视图仅提供可视内容,因此独立于数据源。您可以使用它们来提供自定义背景,填充单元格周围的空间,甚至可以根据需要隐藏单元格。装饰视图仅由布局对象定义和管理,不与集合视图的数据源对象交互。

如果你想说,在你的集合视图后面添加一个背景图片,它的显示仍然与数据无关,那么装饰视图会更合适。

页眉,页脚和其他补充视图可以使用数据源通过以下方法更新:

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

他们的定位和布局可以通过覆盖来控制

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

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

从您的自定义流程布局中。

在我看来,这是解决问题的最佳方法,如果您不想手动查找和删除装饰视图,将用补充视图替换您的装饰视图,这样就可以使用数据来源。

相关问题