UICollectionView didSelectItemAtIndexPath滚动

时间:2014-08-05 06:00:22

标签: ios objective-c scroll collectionview

我正在使用CollectionView来显示图像。我正在使用启用分页的水平滚动,因此用户可以像在iOS上的应用程序屏幕上一样从右到左滚动图像。我怎样才能调用didSelectItemAtIndexPath来检测集合视图停止在哪个单元格而无需用户点击单元格?

虽然不相关:

- (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);

}

和图像

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

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

非常感谢你!

1 个答案:

答案 0 :(得分:0)

您可以在集合视图上调用visibleCells,以便在屏幕上显示当前可见的单元格,如下所示:

// 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;