kye*_*kye 20 ios uicollectionview swift
我有一个启用分页的UICollectionView内部UIViewController.由于一些奇怪的原因,collectionView.scrollToItem当方向collectionview是,vertical但不是方向时工作horizontal.这有什么我做错了或者这应该发生吗?
//Test scrollToItem
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let i = IndexPath(item: 3, section: 0)
collectionView.reloadData()
collectionView.scrollToItem(at: i, at: .top, animated: true)
print("Selected")
}
Run Code Online (Sandbox Code Playgroud)
Lun*_*irl 47
显然有一个新的错误UICollectionView导致scrollToItem在启用分页时无法工作。解决方法是在调用之前禁用分页scrollToItem,然后在调用之后重新启用它:
collectionView.isPagingEnabled = false
collectionView.scrollToItem(
at: IndexPath(item: value, section: 0),
at: .centeredHorizontally,
animated: true
)
collectionView.isPagingEnabled = true
Run Code Online (Sandbox Code Playgroud)
来源:https : //developer.apple.com/forums/thread/663156
kl.*_*oon 31
对于这部分:
collectionView.scrollToItem(at: i, at: .top, animated: true)
当horizontal您需要使用滚动方向时at: left,at: right或at: centeredHorizontally.at: top是为了vertical方向.
Ked*_*kar 10
斯威夫特 5.1,Xcode 11.4
collectionView.scrollToItem(at: IndexPath(item: pageNumber , section: 0), at: .centeredHorizontally, animated: true)
self.collectionView.setNeedsLayout() // **Without this effect wont be visible**
Run Code Online (Sandbox Code Playgroud)
我在流程布局中实现此功能时遇到了问题,每个项目都输入了分页..centeredHorizontally对我来说不起作用所以我使用scroll to rect并在滚动之前检查数据:
if self.collectionView?.dataSource?.collectionView(self.collectionView!, cellForItemAt: IndexPath(row: 0, section: 0)) != nil {
let rect = self.collectionView.layoutAttributesForItem(at: IndexPath(item: data[index], section: 0))?.frame
self.collectionView.scrollRectToVisible(rect!, animated: false)
}
Run Code Online (Sandbox Code Playgroud)
小智 9
我尝试了很多事情但都失败了。这节省了我的时间。
func scrollToIndex(index:Int) {
let rect = self.collectionView.layoutAttributesForItem(at: IndexPath(row: index, section: 0))?.frame
self.collectionView.scrollRectToVisible(rect!, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
参考:https ://stackoverflow.com/a/41413575/9047324
对我来说,我必须在主线程上滚动集合视图,例如:
DispatchQueue.main.async {
self.collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .centeredHorizontally, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
小智 5
将项目添加到 collectionView 和 后reloadData(),scrollToItem由于重新加载数据尚未完成而无法正常工作。因此,我像这样添加了 performBatchUpdates :
self.dataSource.append("Test")
self.collectionView.performBatchUpdates ({
self.collectionView.reloadData()
}, completion: { _ in
self.collectionView.scrollToItem(at: IndexPath(item: 3, section: 0),
at: .centeredHorizontally,
animated: false)
})
Run Code Online (Sandbox Code Playgroud)
我知道这不是关于这个问题,但它会对这个标题有所帮助。
我有这个问题:当点击按钮(水平集合滚动到下一个项目)时,它总是返回到第一个带有 UI 错误的项目。
原因在这个参数中: myCollection.isPagingEnabled = true
解决方案:只需在滚动到下一项之前禁用分页,并在滚动后启用它。
小智 5
scrollToItem如果您之前打电话,这将不起作用viewDidLayoutSubviews。使用scrollToIteminviewDidLoad将不起作用。所以viewDidLayoutSubviews()完成后调用这个函数。
| 归档时间: |
|
| 查看次数: |
15788 次 |
| 最近记录: |