UICollectioView:添加单元格后内容偏移错误
UICollectioView: wrong content offset after adding cells
我创建循环 UICollectionView(因此用户不可能滚动到末尾,因为当他滚动到上一行时,我从末尾删除了不正确的行以将它们插入到 UICollectionView 的开头.
在我的 UICollectioView 中,我总是有 11 个单元格。 UICollectionViewCell 与 UICollectionView 具有相同的边界。
问题:例如,我将内容滚动到 row = 2 的 indexPath。如果我在开头添加 2 个单元格并从末尾删除 2 个单元格,我想看到同一个单元格,但它滚动到第二行(我之前看到的那一行),但我不希望 smth 滚动,我想插入 2 行没有任何动画,所以用户应该看到第 4 行,但是它认为它是第二行)
代码如下:
let numberOfRowInCollectionView = collectionView_help_getNumberOfRowsInCollectionView()
collectionViewMain.performBatchUpdates({ [weak self] in
if self == nil {
return
}
let currentRowOffset = 2
let numberOfRowsToAddFromLeft = currentRowOffset // number of rows we need to add or remove from left
var array_indexPathToAdd = [NSIndexPath]()
var array_indexPathToRemove = [NSIndexPath]()
for i in 0..<numberOfRowsToAddFromLeft {
array_indexPathToAdd.append(NSIndexPath(forRow: i, inSection: 0))
array_indexPathToRemove.append(NSIndexPath(forRow: numberOfRowInCollectionView - 1 - i, inSection: 0))
}
self?.collectionViewMain.insertItemsAtIndexPaths(array_indexPathToAdd)
self?.collectionViewMain.deleteItemsAtIndexPaths(array_indexPathToRemove)
self!.selectedRow += currentRowOffset
}, completion: { (finished) in
completionBlock?()
})
我尝试添加:
// select new row
let selectedIndexPath = NSIndexPath(forRow: self!.selectedRow, inSection: 0)
self!.collectionViewMain.scrollToItemAtIndexPath(selectedIndexPath, atScrollPosition: .CenteredHorizontally, animated: false)
然后它会选择适当的新行(现在它 = 4),但它会使用动画来让用户看到正在发生的事情。
但我只想在开头添加 2 个单元格,用户不应该看到正在发生的事情。
我用这个源码实现了这个循环的UICollectionViewGit
这个想法不是在 UICollectionView 中使用删除和插入单元格,而是有额外的 0(数组中最后一个对象的数据)和最后(数组中 0 个对象的数据)行并设置内容偏移量而不动画。
我创建循环 UICollectionView(因此用户不可能滚动到末尾,因为当他滚动到上一行时,我从末尾删除了不正确的行以将它们插入到 UICollectionView 的开头.
在我的 UICollectioView 中,我总是有 11 个单元格。 UICollectionViewCell 与 UICollectionView 具有相同的边界。
问题:例如,我将内容滚动到 row = 2 的 indexPath。如果我在开头添加 2 个单元格并从末尾删除 2 个单元格,我想看到同一个单元格,但它滚动到第二行(我之前看到的那一行),但我不希望 smth 滚动,我想插入 2 行没有任何动画,所以用户应该看到第 4 行,但是它认为它是第二行)
代码如下:
let numberOfRowInCollectionView = collectionView_help_getNumberOfRowsInCollectionView()
collectionViewMain.performBatchUpdates({ [weak self] in
if self == nil {
return
}
let currentRowOffset = 2
let numberOfRowsToAddFromLeft = currentRowOffset // number of rows we need to add or remove from left
var array_indexPathToAdd = [NSIndexPath]()
var array_indexPathToRemove = [NSIndexPath]()
for i in 0..<numberOfRowsToAddFromLeft {
array_indexPathToAdd.append(NSIndexPath(forRow: i, inSection: 0))
array_indexPathToRemove.append(NSIndexPath(forRow: numberOfRowInCollectionView - 1 - i, inSection: 0))
}
self?.collectionViewMain.insertItemsAtIndexPaths(array_indexPathToAdd)
self?.collectionViewMain.deleteItemsAtIndexPaths(array_indexPathToRemove)
self!.selectedRow += currentRowOffset
}, completion: { (finished) in
completionBlock?()
})
我尝试添加:
// select new row
let selectedIndexPath = NSIndexPath(forRow: self!.selectedRow, inSection: 0)
self!.collectionViewMain.scrollToItemAtIndexPath(selectedIndexPath, atScrollPosition: .CenteredHorizontally, animated: false)
然后它会选择适当的新行(现在它 = 4),但它会使用动画来让用户看到正在发生的事情。
但我只想在开头添加 2 个单元格,用户不应该看到正在发生的事情。
我用这个源码实现了这个循环的UICollectionViewGit
这个想法不是在 UICollectionView 中使用删除和插入单元格,而是有额外的 0(数组中最后一个对象的数据)和最后(数组中 0 个对象的数据)行并设置内容偏移量而不动画。