【问题标题】:Delete specific items in UICollectionView删除 UICollectionView 中的特定项目
【发布时间】:2018-09-20 01:56:04
【问题描述】:

我想删除 UICollectionView 中具有特定 ID 的单元格。所以单元格可以在视图的中间。

为此,我从单元格数组中删除了该项目,并删除了 indexPath。必须首先检索 indexPath。这是不可能的!

无法删除不可见/隐藏的单元格(在当前视图下方,必须向上滚动),因为它们结果是 NIL。

如何访问某个单元格(带有 ID)并将其删除?

    let cellIdToFeedItem_ToDelete = [ String : FeedItem ]() // here are the item to be deleted
    var indexPathsToRemove        = [ IndexPath ]()
    var indexPathsAllActive       = [ IndexPath ]() // Will be filled below

    // --- retrieving all indexPaths
    if let nrSections = self.collectionView?.numberOfSections
    {
        for s in 0..<nrSections
        {
            if let v = self.collectionView?.numberOfItems( inSection: s )
            {
                for i in 0..<v
                {
                    let indexPathTMP = IndexPath( item: i, section: s )
                    indexPathsAllActive.append( indexPathTMP )
                }
            }
        }
    }

    // --- delete the items with the same
    for indexPath in indexPathsAllActive
    {
        // #### FOR HIDDEN CELLS THIS IS 'NIL'
        let cell = self.collectionView?.cellForItem( at: indexPath ) as? FeedCollectionViewCell

        if let cellID = cell?.id
        {
            if let _ = cellIdToFeedItem_ToDelete[ cellID ]
            {
                if let index = feedItemsActive_.firstIndex( where: {
                    (i) -> Bool in
                    i.id == cellID
                } )
                {
                    feedItemsActive_.remove( at: index )
                    indexPathsToRemove.append( indexPath )
                }
            }
        }
    }

    self.collectionView?.deleteItems( at: indexPathsToRemove )

【问题讨论】:

  • 你为什么要得到细胞?单元格是视图,而不是您的数据。遍历您的数据模型,而不是单元格。
  • 我正在获取单元格以获取 ID。我应该迭代什么来发现我正在删除正确的?
  • 正如我所说,迭代你的数据模型。这就是 id 最初的来源。

标签: ios swift uicollectionview


【解决方案1】:

从您的数据源中删除项目(我假设是 indexPathsAllActive?)然后重新加载您的 collectionView 以显示更改。

没有动画:

collectionView.reloadData()

带动画:

collectionView.performBatchUpdates{ }

以下是您在代码中使用 performBatchUpdates 的方式:(如果您有一个部分)

collectionView?.performBatchUpdates({
    var targetIndexPaths: [IndexPath] = []
    for ( cellID, _ ) in cellIdToFeedItem_ToDelete
    {
        if let index = feedItemsActive_.firstIndex( where: { $0.id == cellID } )
        {
            feedItemsActive_.remove( at: index )
            targetIndexPaths.append(IndexPath(item: index, section: 0))
        }
    }
    collectionView?.deleteItems(at: targetIndexPaths]) 
}, completion: nil)

【讨论】:

  • reloadData() 太苛刻了,performBatchUpdates() 会带来更好的用户体验。
  • performBatchUpdated{ } 当我从我的数据模型中删除元素而不是单元格时崩溃。这引出了我最初的问题。如何获取所有单元格/索引路径?
  • performBatchUpdates你需要同时更新dataSource和collectionview
  • 是的,我可以看到,但最初的问题是,如何做。因为我无法获取所有 collectionView 项目(indexPaths)。你知道如何得到它们吗?我的代码不检索隐藏元素。显然,.indexPathsForVisibleItems() 也不行。
  • 我把我的样本和你的合并了。如果你有一个部分,它应该很容易适应
【解决方案2】:

这完成了工作。但是没有动画:

    for ( cellID, _ ) in cellIdToFeedItem_ToDelete
    {
        if let index = feedItemsActive_.firstIndex( where: {
            (i) -> Bool in
            i.id == cellID
        } )
        {
            feedItemsActive_.remove( at: index )
        }
    }

    self.collectionView?.reloadData()

【讨论】:

  • 如果你想要动画,不要使用reloadData。使用deleteItems(at:)
  • 这正是我在最初的问题中所做的。但它不起作用。那么如何检索所有 indexPaths?
猜你喜欢
  • 1970-01-01
  • 2016-01-29
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多