0

I am using a CollectionView to display an image in. I am using horizontal scrolling with pagination enabled so the user is able to scroll from image to image right to left as they would on the apps screen on iOS. How might I be able to call didSelectItemAtIndexPath to detect which cell the collection view has stopped on without the user having to tap on the cell?

Though it is not relevant:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    titleLabel.alpha = 1.0;
    mainTextLabel.alpha = 1.0;
    availableLabel.alpha = 1.0;
    sizeLabel.alpha = 0.0;
    sizeImage.alpha = 0.0;
    moreInfoOnSize.alpha = 0.0;

    UICollectionViewCell *datasetCell =[collectionView cellForItemAtIndexPath:indexPath];
    datasetCell.backgroundColor = [UIColor lightGrayColor]; // highlight selection

    PFObject *selectedObject = [labelFileArray objectAtIndex:indexPath.row];
    titleLabel.text = selectedObject[@"labelText"];

    PFObject *selectedObject2 = [mainTextArray objectAtIndex:indexPath.row];
    mainTextLabel.text = selectedObject[@"mainTextLabel"];

    PFObject *selectedObject3 = [availableLabelArray objectAtIndex:indexPath.row];
    availableLabel.text = selectedObject[@"availableLabel"];
    if ([availableLabel.text isEqual:@"Available"]) {
        availableLabel.backgroundColor = [UIColor greenColor];
    } else if ([availableLabel.text isEqual:@"Sorry We Are Out!"])
    {
        availableLabel.backgroundColor = [UIColor redColor];
    }

    NSLog(@"%@", indexPath);

}

And Image

http://cl.ly/image/3x1R41073W2z

http://cl.ly/image/203Q2E3T2X44

Thank you so much!

Nicholas
  • 149
  • 1
  • 13
  • `didSelectItemAtIndexPath` is called specifically when a user selects (taps on and releases) a cell. What are you trying to do? – Dima Aug 05 '14 at 06:06
  • Correct. So basically I need to have didSelectItemAtIndexPath (or something else) after the use swipes through the collection view. So lets say that image 1 is showing.. once the user swipes to the right image 2 will be showing but I need to have that called as if they just selected image 2. Does that make sense? – Nicholas Aug 05 '14 at 06:10
  • You could call `visibleCells` on the collection view after it finishes scrolling to get all the cells currently on the screen. Then you can do whatever you want with those. – Dima Aug 05 '14 at 06:12
  • Okay now I am filling these cells with data (an image) from Parse.. does 'visibleCells' return a number or actual value of the cell? As you might notice I am new here but I think you may be right! Would you like to post an answer maybe with some code? haha again thank you so much! – Nicholas Aug 05 '14 at 06:17
  • I just posted an answer with how to get the visible cells and grab the first cell from the array. – Dima Aug 05 '14 at 06:20

1 Answers1

0

You can call visibleCells on your collection view to get the cells currently visible on the screen like this:

// All visible cells (1 or more depending on your layout and cell sizes)
NSArray *visibleCells = [myCollectionView visibleCells];

// Get the first cell from the array
UICollectionViewCell *firstCell = visibleCells.firstObject;
Dima
  • 23,484
  • 6
  • 56
  • 83