如何使用 swift 重置 UICollectionView 滚动位置?
How can I reset UICollectionView scroll position using swift?
我正在使用一个带有 UITableViewCells 的 UITableView,其中有一个 UICollectionView。每个 UICollectionView 中的第一项是一个 header 单元格,我不想关注它。
func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
if indexPath.row == 0 {
// TODO: scroll to correct position here
return false
}
return true
}
所以我将关注的最左边是 header 单元格(在索引 1 处)右侧的第一个单元格。出现的问题是 - 当向右滚动时,单元格滚动到屏幕外,我向左滚动,因为我无法专注于第一个项目,header 单元格永远在屏幕外,因为滚动位置不会'更新。如何以编程方式更新滚动位置?
添加这一行是有效的。在这种情况下,indexPath 等于第一个单元格的 NSIndexPath。
collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
我正在使用一个带有 UITableViewCells 的 UITableView,其中有一个 UICollectionView。每个 UICollectionView 中的第一项是一个 header 单元格,我不想关注它。
func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
if indexPath.row == 0 {
// TODO: scroll to correct position here
return false
}
return true
}
所以我将关注的最左边是 header 单元格(在索引 1 处)右侧的第一个单元格。出现的问题是 - 当向右滚动时,单元格滚动到屏幕外,我向左滚动,因为我无法专注于第一个项目,header 单元格永远在屏幕外,因为滚动位置不会'更新。如何以编程方式更新滚动位置?
添加这一行是有效的。在这种情况下,indexPath 等于第一个单元格的 NSIndexPath。
collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)