Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 确定UICollectionViewCell中的抽头元素_Ios_Objective C_Ios7_Uikit - Fatal编程技术网

Ios 确定UICollectionViewCell中的抽头元素

Ios 确定UICollectionViewCell中的抽头元素,ios,objective-c,ios7,uikit,Ios,Objective C,Ios7,Uikit,我有一个带有自定义单元格的UICollectionView。当我点击一个单元格时,我会看到一个集合视图:didSelectItemAtIndexPath: 这不允许我确定单元格内的哪个元素被点击(图像或标签)。如何确定单元格中的哪个元素被点击?在cellForItem中创建单元格时,只需向单元格元素添加uitagesturerecognizer,并添加要调用的目标和选择器。然后,选择器方法将获得一个具有UIView属性的识别器,该属性将是您选择的元素。您必须将UICollectionViewCe

我有一个带有自定义单元格的
UICollectionView
。当我点击一个单元格时,我会看到一个
集合视图:didSelectItemAtIndexPath:


这不允许我确定单元格内的哪个元素被点击(图像或标签)。如何确定单元格中的哪个元素被点击?

cellForItem
中创建单元格时,只需向单元格元素添加
uitagesturerecognizer
,并添加要调用的目标和选择器。然后,选择器方法将获得一个具有
UIView
属性的识别器,该属性将是您选择的元素。

您必须将
UICollectionViewCell
子类化,并在
uiCollectionView
中包含此类对象。然后在单元格中创建一个
委托
,方法如下

- (void)collectionCell:(UICollectionViewCell *)cell didTapButtonWithIndex:(NSUInteger)index

并将视图控制器设置为每个单元格的代理。因此,您可以在这些方法中获得操作,而不是
collectionView:didSelectItemAtIndexPath:

您可以在整个单元格中设置一个点击
UIAppgestureRecognitizer
,并使用
-(UIView*)hitTest:(CGPoint)PointwithEvent:(UIEvent*)事件以获取点击的对象

-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath中
这样做

单元中点击

-(void)cellTapped:(UIGestureRecognizer *)gesture
{
    CGPoint tapPoint = [gesture locationInView:gesture.view];
    UIView *tappedView = [gesture.view hitTest:tapPoint withEvent:nil];

    if ([tappedView isKindOfClass:[UILabel class]]) {
        NSLog(@"Found");
    }
}

请检查是否在单元格和各个子视图(如标签和imageview)上设置了用户交互。希望这有帮助。

我没有看到UICollectionViewCell有委托。我查看了Apple的IOS参考,没有看到任何这样的委托方法:
(void)collectionCell:(UICollectionViewCell*)cell didTapButtonWithIndex:(NSInteger)index
。它不存在,您必须创建此协议
-(void)cellTapped:(UIGestureRecognizer *)gesture
{
    CGPoint tapPoint = [gesture locationInView:gesture.view];
    UIView *tappedView = [gesture.view hitTest:tapPoint withEvent:nil];

    if ([tappedView isKindOfClass:[UILabel class]]) {
        NSLog(@"Found");
    }
}