Skip navigation

UICollectionView - programatically setting the selected item and firing a delegate method (ideally)

629 Views 1 Reply Latest reply: Jan 10, 2013 5:05 AM by PunkStar Studios RSS
PunkStar Studios Calculating status...
Currently Being Moderated
Jan 9, 2013 8:27 AM

HI There.

So I've got a UICollection view subclassed nicely.

Shows images and doed some nice zooming and snapping effects.


The problem I am having, is that I am not able to programatically select the displayed/scrolled to item.


For example, in my subclassed FlowLayout class I can detect when I am centered with :


-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{

    NSArray* array = [super layoutAttributesForElementsInRect:rect];

    CGRect visibleRect;

    visibleRect.origin = self.collectionView.contentOffset;

    visibleRect.size = self.collectionView.bounds.size;

    for(UICollectionViewLayoutAttributes* attributes in array){

        if(CGRectIntersectsRect(attributes.frame, rect)){

            CGFloat distance = CGRectGetMidX(visibleRect) -;

            if(CGRectGetMidX(visibleRect) =={

                //It's now centered!!!

                NSLog([NSString stringWithFormat:@"Scrolled stopped at : %@",attributes.indexPath],nil);


                [self.collectionView selectItemAtIndexPath:attributes.indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];



     ... etc...



The index path is correct and all that - but that "selectItemAtIndexPath" by Apple;s design sets the selected item but does not fire any delegate methods.

What I want to do is fire some code to lookup info in the array for that item that's been centered.


Look at the starbucks app where you manage your virtual cards. As you scroll through your cards - data on the screen like balance etc. is automatically filled out. So far I am not able to do that - the user if forced to tap on the selection after scrolling to to - just the act of scrolling doesn't auto select the item.


It would have been perfect if the didSelectItemAtIndexPath: delegate method was called but no such luck **** it.


I thought of somehow registering an action selector and firing that by passing the attributes.indexpath but I have not gotten that to work.


Can someone help me here?


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.