简体   繁体   中英

UICollectionView didSelectItemAtIndexPath Scrolling

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!

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM