How to select item in collectionview programmatically?

67,173

Solution 1

You can simply use this after [self.collectionView reloadData]

[self.collectionView 
   selectItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] 
                animated:YES
          scrollPosition:UICollectionViewScrollPositionCenteredVertically];

where index is the index number for the selected student.

Solution 2

Swift

let indexPath = self.collectionView.indexPathsForSelectedItems?.last ?? IndexPath(item: 0, section: 0)
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionView.ScrollPosition.centeredHorizontally)

Solution 3

From your question I'm assuming you only ever have one selected student, I did a similar thing with a collection of icons the user could select. Firstly in view did load I did:

override func viewDidLoad() {
    super.viewDidLoad()

    iconCollectionView.delegate = self
    iconCollectionView.dataSource = self
    iconCollectionView.allowsMultipleSelection = false
    iconCollectionView.selectItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), animated: false, scrollPosition: .None)
}

Here I default to selecting the first cell, you would use StudentArray.indexOf to get your selected student index. Then to show the which item is selected I did:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = iconCollectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! IconCollectionViewCell
    cell.imageView.image = UIImage(named: imageResourceNames.pngImageNames[indexPath.row])
    if cell.selected {
        cell.backgroundColor = UIColor.grayColor()
    }
    return cell
}

This is called when the collection is first displayed, then to react to changes in selection:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.grayColor()
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.clearColor()
}

Obviously there are many ways to show cell selection, my way is simple but that's all I needed to do.

EDIT: Since posting the above I have realised it's simpler to just add an observer to selected in the cell class:

class IconCollectionViewCell: UICollectionViewCell {

...

    override var selected: Bool {
        didSet {
            backgroundColor = selected ? UIColor.grayColor() : UIColor.clearColor()
        }
    }
}

With this in place the there is no need to handle didSelect or didDeselect or check for selected in cellForItemAtIndexPath, the cell does it automatically.

Solution 4

Super late I know, but just for Record, for item x in section y:

 NSIndexPath *indexPath = [NSIndexPath indexPathForItem:x inSection:y];

if you want to just highlight an item programmatically, you need to call selectItemAtIndexPath method on the collection view itself like:

[theCollectionView selectItemAtIndexPath:indexPath
    animated:YES scrollPosition:UICollectionViewScrollPositionCenteredVertically];

if you want to simulate selecting an item programmatically and you get the code in did select gets called, you need to call selectItemAtIndexPath method on the view controller itself and pass to it your collection view as param like:

[theViewController collectionView:theCollectionView didSelectItemAtIndexPath:indexPath];

Now as sure thing, in order to get an item selected and the did select code gets called, you need to call both :D

[theCollectionView selectItemAtIndexPath:indexPath
    animated:YES scrollPosition:UICollectionViewScrollPositionCenteredVertically];

[theViewController collectionView:theCollectionView didSelectItemAtIndexPath:indexPath];

SWIFT version:

let indexPath = IndexPath(item: x, section: y)
theCollectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredVertically)
theViewController.collectionView(theCollectionView, didSelectItemAt: indexPath)

enjoy!

Solution 5

the signature has changes.

didSelectItemAtIndexPath  to didSelectItemAt 

you check this please:

_ collectionView

try with this please:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){

        print(" selected",)
    }
Share:
67,173

Related videos on Youtube

mrd
Author by

mrd

Java and Android Development. PHP, Java, Android, iOS, Laravel, MySQL

Updated on July 09, 2022

Comments

  • mrd
    mrd almost 2 years

    I have a UICollectionView. The UICollectionView's datasource is a NSArray of students (from CoreData). I want the current student item be selected/highlighted.

    How can I do this? I know there is a method:

    - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath 
                         animated:(BOOL)animated 
                   scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
    

    which takes as arguments an NSIndexPath and UICollectionViewScrollPosition.

    I have the datasource (NSArray of students populated from CoreData) and the student object to be selected.

    So, how do I get the NSIndexPath and UICollectionViewScrollPosition? Or is there any other way to highlight an item?

    • Tim
      Tim over 10 years
      Your datasource should contain values that store whether or not an item is selected. Update those, then just reload the Collection View.
    • PAULMAX
      PAULMAX about 3 years
      my version is tested works 100% stackoverflow.com/a/67161317/11359553
  • mrd
    mrd over 10 years
    Didn't work for me, because the didSelectItemAtIndexPath is called, too and somehow this interferes. My solution was to highlight the current student cell in cellForItemAtIndexPath, and when the user switches to a different student, to un-highlight all cells, which are not selected in didDeselectItemAtIndexPath.
  • Shahin
    Shahin over 10 years
    hmm, you can check the item in the didSelectItemAtIndexPath to see if the selected student index is equal to the index of the selected row. if it is not, deselect other rows. Am I right?
  • Krishna
    Krishna over 7 years
    Late to the party, but it can help people. From Apple's documentation for optional func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath), The collection view calls this method when the user successfully selects an item in the collection view. It does not call this method when you programmatically set the selection.
  • cabyambo
    cabyambo about 5 years
    Apples documentation says the following: "This method does not cause any selection-related delegate methods to be called." When calling the select function the delegate methods won't get called, so there will be no visual cue that your first cell has been selected.
  • IsPha
    IsPha over 4 years
    Hi all, check my answer plz stackoverflow.com/a/60863297/2715840
  • Andy Weinstein
    Andy Weinstein about 3 years
    I use this to preinitialize the state of a collectionView while it is being created. To get it to work you need to call the code described inside a DispatchQueue.main.async {() in Code goes here}