【发布时间】:2022-01-13 04:31:12
【问题描述】:
我在我的项目中实现了分页,并注意到在插入新数据和重新加载集合视图时,我会从第 52 项转到第 34 项。有没有办法留在第 52 项并继续而不是回到第 34 项?任何帮助将不胜感激
【问题讨论】:
我在我的项目中实现了分页,并注意到在插入新数据和重新加载集合视图时,我会从第 52 项转到第 34 项。有没有办法留在第 52 项并继续而不是回到第 34 项?任何帮助将不胜感激
【问题讨论】:
您可以尝试以下不同的解决方案
您可以为最后一个可见索引重新加载集合
yourCollectionView.reloadItems(at: collectionView.indexPathsForVisibleItems)
添加新项目时调用下面的方法 || 关于细胞选择 || 从任何其他方法从您要更新集合项的位置获取您要查看的索引路径并传递给方法
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)
}
希望这个解决方案可以帮助你?
【讨论】: