2

How can I achieve that the images which comes into view is automatically selected?

I know that I can have the first item in the collection view selected by default, but how to do it with every item that comes into view? To explain it in more detail: I have an imageview depending on the selection of the collectionView. What I want to do is, that the user don't have to select the collectionViewcell to get the depending imageview. I want the user to just swipe through the collection view and the image that comes up is selected and an overview shown in the depending imageview. The main problem is in my opinion, that I need to Ave just one selected at a time.

3 Answers 3

3

You can try to enable multiple selection:

collectionView.allowsMultipleSelection = true

And then in a loop select items that you want to have selected:

collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .top)

EDIT

You can use willDisplay delegate method of the UICollectionViewDelegate to do the selection when the cell is being displayed:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    // this is called before the cell is displayed, check if this is the cell to be selected, and if yes, select it:
    collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .top)
}
1
  • Thank you for your quick response! I thought about something like that, but I mustn't select more than one item at a time. Is there a method for collection view which is called when the cell is in view? Commented Feb 6, 2018 at 21:39
1

As far as I understood your question try this selecting an item in the cellForItemAt indexPath. So as soon as the cell is loaded (shown) you select it.

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    [...]

    collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .top)

    [...]
}

Also as mentioned in other answer: collectionView.allowsMultipleSelection = true

4
  • Thank you for your response. I tried the method you showed above, but it doesn't do what I hoped it will do. To explain it in more detail: I have an imageview depending on the selection of the collectionView. What I want to do is, that the user don't have to select the collectionViewcell to get the depending imageview. I want the user to just swipe through the collection view and the image that comes up is selected and an overview shown in the depending imageview. Commented Feb 6, 2018 at 21:50
  • to be honest i didn't understand you:) do you have a video that shows it? or some app available on app store that does it and i can check that UX there?
    – Tung Fam
    Commented Feb 6, 2018 at 21:56
  • Sorry no I have no video for it. Just think of a coverflow and depending on the cover the artist is shown. But the willDisplay method was the one I was looking for. I very appreciated your help! Commented Feb 6, 2018 at 22:04
  • great! glad you found it!
    – Tung Fam
    Commented Feb 7, 2018 at 12:21
1

I think you should use collectionView:willDisplayCell:forItemAtIndexPath: rather than the cellForItemAtIndexPath: (mentioned above) to call the selectItem(at:animated:scrollPosition:), this will ensure the cell is actually about to show.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.