2014-05-24 3 views
2

사용자 정의 셀에 UICollectionView이 있습니다. 내가 셀을 탭하면 collectionView: didSelectItemAtIndexPath:이됩니다.UICollectionViewCell에서 탭 요소를 결정하십시오.

이렇게하면 셀 안의 어떤 요소 (이미지 또는 레이블)를 탭했는지 확인할 수 없습니다. 세포 내의 어떤 요소가 두드려 졌는지 확인하는 방법?

답변

1

UICollectionViewCell의 서브 클래스를 만들고 UICollectioView에 이러한 종류의 개체가 있어야합니다. 그럼 당신은

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

같은 방법으로 세포의 delegate를 만들고이 셀의 각각의 대리인으로보기 컨트롤러를 설정합니다. 그래서 당신은 당신이 전체 셀에 탭 UITapGestureRecognizer을 설정할 수 있습니다 대신 collectionView: didSelectItemAtIndexPath:

+0

UICollectionViewCell에 대리자가 있다는 것을 알 수 없었습니다. 나는 애플의 IOS 레퍼런스를 체크했는데,'(void) collectionCell : (UICollectionViewCell *) cell didTapButtonWithIndex : (NSUInteger) index'와 같은 위임 메서드를 보지 못했습니다. –

+0

존재하지 않으므로이 프로토콜을 만들어야합니다. – Levi

1

cellForItem에 셀을 만들 때 UITapGestureRecognizer을 셀 요소에 추가하고 target 및 selector를 호출하여 호출합니다. selector 메서드는 UIView 속성을 가진 인식자를 가져 오며이 속성은 선택한 요소가됩니다.

2

이러한 방법의 조치를 얻을이

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellTapped:)]; 
    tapGesture.numberOfTapsRequired = 1; 
    [cell addGestureRecognizer:tapGesture]; 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 에서 도청 대상에게

를 얻을 수 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;을 사용 그리고 안으로 cellTapped

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

라벨과 이미지 뷰와 같은 개별 하위 뷰와 셀에 사용자 상호 작용이 설정되어 있는지 확인하십시오. 희망이 도움이됩니다.

관련 문제