UICollectionView is scrolling when selecting a cel

2019-04-05 03:54发布

I'm having trouble when i try to select an item inside a UICollectionView since when I click on it it scrolls a little bit.

I know didSelectItemAtIndexPath is being called but I want to prevent the scrolling when selecting. I only want the collection view to scroll when the user is scrolling through but if the user is just tapping the cell it shouldn't move. Only should be selected.

I hope you can help me since I don't know how to prevent this problem.

Any help will be really appreciated.

4条回答
Root(大扎)
2楼-- · 2019-04-05 04:08

For Objective-C

[self.collectionView selectItemAtIndexPath:path animated:NO scrollPosition:UICollectionViewScrollPositionNone] 

For Swift

collectionview.selectItemAtIndexPath(indexPath: path, animated: false, scrollPosition: None)
查看更多
成全新的幸福
3楼-- · 2019-04-05 04:26

This can happen if you have paging enabled and the CollectionView is manually scrolled to a position that doesn't align with the expected page boundaries. When you select the cell, it adjusts to put the CollectionView at the correct page boundaries.

查看更多
对你真心纯属浪费
4楼-- · 2019-04-05 04:28

If you are selecting the cell programmatically with collectionview.selectItem(at: indexpath, animated: true, scrollPosition: .top) -- and since you didn't share any of your code with us, let's assume that's correct...

... then like me you might not have realized that you can use an empty set like this: collectionview.selectItem(at: indexpath, animated: true, scrollPosition: [])

查看更多
倾城 Initia
5楼-- · 2019-04-05 04:32

You can try this:

collectionView.selectItem(at: newIdexPath, animated: true, scrollPosition: UICollectionViewScrollPosition(rawValue: 0))
查看更多
登录 后发表回答