如何处理UICollectionView自定义单元格的事件?

时间:2014-12-02 10:35:26

标签: ios

我为UICollectionView创建了一个自定义单元格。自定义单元格有两个UIImageView - imageView1和imageView2。

我应该如何处理每个imageView的事件?我只能处理UICollectionView的didSelectedItemIndex委托上的单元格事件。我想获得每个imageView的处理。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotosViewCell *cell = (PhotosViewCell*)[collectionView cellForItemAtIndexPath:indexPath];

 int tag1 = cell.imageView1.tag;
 int tag2 = cell.imageview2.tag;

 ....
}

1 个答案:

答案 0 :(得分:0)

在您的cellForRowAtIndexPath方法中,使用选择器定义点击手势识别器并将其指定给您的图像。

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(productImageTap:)];
[cell.imageView1 addGestureRecognizer:tapGesture];
[cell.imageView1 setUserInteractionEnabled:YES];
[cell.imageView2 addGestureRecognizer:tapGesture];
[cell.imageView2 setUserInteractionEnabled:YES];

然后你可以在productImageTap方法中做任何你想做的事情。只需通过locationInView方法获取indexPath ..

- (void)productImageTap:(UITapGestureRecognizer *)gesture
{
    CGPoint point = [gesture locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
    PhotosViewCell *cell = (PhotosViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //do whatever you want with cell properties.. 
}