我有水平UICollectionView,我想重置它X位置。我试过以下:
let indexPath = IndexPath(row: 0, section: 1)
self.collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition(rawValue: 0), animated: false)
然而它不起作用,我收到了一个错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: <NSIndexPath: 0x61000002d040> {length = 2, path = 1 - 0}'
如何滚动集合视图以启动?
答案 0 :(得分:3)
如果没有项目
,这将无效 self.collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition(rawValue: 0), animated: false)
使用(目标C):
CGPoint topOffest = CGPointMake(0,-self.collectionView.contentInset.top);
[self.collectionView setContentOffset:topOffest animated:YES];
使用(斯威夫特):
let topOffest:CGPoint = CGPoint(0,-self.collectionView.contentInset.top)
collectionView?.setContentOffset(topOffest, animated: true)
答案 1 :(得分:1)
self.collectionView.scrollToItem(at: [0,5], at:.centeredHorizantally, animated: false)
0表示第0节
5代表5号细胞