ios - scrollToItem 在我的 CollectionView 中不起作用?
问题描述
为什么 scrollToItem 在我的 CollectionView 中不起作用?我想在 scrollToItem 上工作EnabledID
。那么什么是问题以及如何解决呢?
代码:
var dataArray = NSMutableArray() // dataArray is has a data from Database
var EnabledID = Int()
EnabledID = UserDefaults.standard.integer(forKey: "EnabledID")
if EnabledID == 0 {
EnabledID = 1
UserDefaults.standard.set(1, forKey: "EnabledID")
} else {
if EnabledID < dataArray.count {
let index = NSIndexPath(item: EnabledID, section: 0)
self.myCollectionView.scrollToItem(at: index as IndexPath, at: .centeredVertically, animated: true)
}
}
解决方案
试试这个代码...
[collectionView setDelegate:self];
[collectionView reloadData];
[collectionView layoutIfNeeded];
[collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
斯威夫特 - 4.x
collectionView.delegate = self
collectionView.reloadData()
collectionView.layoutIfNeeded()
collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: true)
当滚动方向为水平时,您需要使用left、right或centeredHorizontally。
顶部是垂直方向。
collectionView.scrollToItem(at: index, at: .top, animated: true)
推荐阅读
- flutter - 错误:根据闭包上下文的要求,返回类型“NetworkImage”不是“Widget”
- sql - 如何修复案例陈述中的 PLS-00103 错误?
- go - Go REST API 无法从 Apache2 访问
- reactjs - “响应”类型的参数不可分配给“SetStateAction”类型的参数
- sql-server - SQL Server(日期时间)中真正的准确性意味着什么
- angular - Angular Videojs 找不到此媒体的兼容源
- sql - 如何动态地使用 SQL Server 对不同的列求和并将求和结果集用于另一列求和
- python - 尝试获取 request.user 以在表单中使用时未定义名称
- r - eigen() 和正确的特征向量
- node.js - Piscina 的谷歌云运行中的路径名