即使在删除部分数据并重新加载 UICollectionView 后,部分标题仍然存在

Posted

技术标签:

【中文标题】即使在删除部分数据并重新加载 UICollectionView 后,部分标题仍然存在【英文标题】:Section header persists even after removing section data and reloading UICollectionView 【发布时间】:2016-01-21 01:36:25 【问题描述】:

删除部分中的最后一项后,目标是从UICollectionView. 中删除整个部分,包括其标题。不幸的是,即使底层数据模型中没有部分,部分标题仍然存在。

刷新视图 -- 通过从导航堆栈中弹出并导航回视图 -- 正确显示 UICollectionView 并删除了部分标题。

在测试用例中,只有一个部分,因此UICollectionView 在删除最后一项后应该变为空白。

建议?

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int 
    print("# sections: \(USER.getSections().count)")
    return USER.getSections().count


    // Delete item
    let indexPath = view.indexPathsForSelectedItems()![0]
    let section = USER.getSections()[indexPath.section]
    USER.removeItemAt(section, index: indexPath.row)
    view.deleteItemsAtIndexPaths([indexPath])

    // Update one section or entire view
    if (section.getNumItems() > 0) 
        view.reloadSections(NSIndexSet(index: indexPath.section))
     else 
        view.deleteSections(NSIndexSet(index: indexPath.section))
        view.reloadSections(NSIndexSet(index: indexPath.section))
        view.reloadData()
    

【问题讨论】:

numberOfSectionsInCollectionView 的代码? 嗯,现在检查一下是否有错误。谢谢! @beyowulf 你能发帖作为答案吗?那是正确的建议。 :) @beyowulf 还请在答案中包括您必须删除带有 UICollectionView 的部分,否则会收到与Invalid update: invalid number of sections.相关的错误 【参考方案1】:

当您想从 UICollectionView 中删除部分时,您必须: 1. 更新您的模型, 2. 在您的集合视图上调用 deleteSections(sections: NSIndexSet) 3. 确保 numberOfSectionsInCollectionView 为您更新的模型返回适当数量的部分。

【讨论】:

感谢您的帮助!删除 UICollectionView 中的最后一部分是否存在潜在问题?因为现在即使我们调用deleteSections(sections: NSIndexSet) 并且数据模型显示正确的部分数量(通过打印numberOfSectionsInCollectionView 中的值确认)也会出现此错误? 错误是'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (0) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).' 嗯,注意到上面写着0 deleted,但我们调用的是deleteSections(sections: NSIndexSet),正如您在更新后的代码中看到的那样。 抱歉还没有更新帖子。是的,我们打电话给view.deleteSections(Set([indexPath.section])),帖子现在显示了。 在执行 deleteSections 后调用 reloadSections。这就是它抛出错误的原因。您已更新模型,但尚未删除该部分。

以上是关于即使在删除部分数据并重新加载 UICollectionView 后,部分标题仍然存在的主要内容,如果未能解决你的问题,请参考以下文章

tableView beginUpdates / endUpdates 阻止重新加载整个表,即使是空的

TableView:是不是可以在不重新加载 tableview 或部分的情况下删除现有行并插入新行?

如何从collectionview重新加载部分

重新加载页面后,Service Worker 显示为已删除

重定向并重新加载,即使是同一页面

Symfony - 删除并重新加载每个测试的所有数据库记录