【问题标题】:How to reload collectionview while still keeping order when adding new data添加新数据时如何在保持秩序的同时重新加载collectionview
【发布时间】:2022-01-13 04:31:12
【问题描述】:

我在我的项目中实现了分页,并注意到在插入新数据和重新加载集合视图时,我会从第 52 项转到第 34 项。有没有办法留在第 52 项并继续而不是回到第 34 项?任何帮助将不胜感激

Gif of problem

【问题讨论】:

    标签: swift uicollectionview


    【解决方案1】:

    您可以尝试以下不同的解决方案

    1. 您可以为最后一个可见索引重新加载集合

      yourCollectionView.reloadItems(at: collectionView.indexPathsForVisibleItems)

    2. 添加新项目时调用下面的方法 || 关于细胞选择 || 从任何其他方法从您要更新集合项的位置获取您要查看的索引路径并传递给方法

       func reloadCollectionView(collectionView: UICollectionView,index:IndexPath){
      
       let contentOffset = collectionView.contentOffset
       collectionView.reloadData()
       collectionView.layoutIfNeeded()
       collectionView.setContentOffset(contentOffset, animated: false)
       collectionView.scrollToItem(at: index, at: .centeredHorizontally, animated: false) 
       }
      

    例如在集合中确实选择了我正在使用以下方式调用此方法的项目

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
          self.reloadCollectionView(collectionView: self.yourCollectionView, index: indexPath)
    
    }
    

    希望这个解决方案可以帮助你?

    【讨论】:

    • 虽然这样解决了保留位置的问题,但是当滚动回到开始时,Collectionview中的Cells开始重叠。有什么解决办法吗?
    猜你喜欢
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多