我有UICollectionView
显示已本地保存到设备的数据。显示此数据后,我会调用服务器来检索任何新数据。
如果服务器有新数据,我需要将它插入数组数据源的开头,因为它是最新的数据。然后我需要刷新UICollectionView
,以便它知道在开头/顶部插入了新数据。
这一切都很好,但问题是动画发生了,用户会注意到。如果用户向下滚动到某些缓存数据,然后将新数据添加到顶部,则他们在屏幕上看到的单元格将重新加载/动画。
我需要在没有动画的情况下保留上述所有内容。应在顶部添加新单元格,而不更改用户正在查看的任何当前单元格。
以下是我用来插入和处理新数据的代码:
self.collectionView.performBatchUpdates({
var index = 0
var indexPaths = [NSIndexPath]()
for newObject in objects {
// Update datasource
self.datasource.insert(newObject, atIndex: index)
// Create index paths for new objects
let indexPath = NSIndexPath(forItem: index, inSection: 0)
indexPaths.append(indexPath)
// Update index
index += 1
}
self.collectionView.insertItemsAtIndexPaths(indexPaths)
}, completion: { (completed) in
})
我尝试在0
函数中将此代码包装在持续时间为UIView.performWithoutAnimation
的UIView动画块中,并且还在我的UICollectionViewFlowLayout
子类中实现了以下内容:
public override func initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
return nil
}
这些都不起作用。动画总是会发生。当插入和添加新数据时,用户正在查看的单元格flash / animate /等。
如何在没有动画的情况下完成此操作?