2014-01-13 1 views
0

UIButton 및 UIImageView가있는 사용자 지정 UITableViewCell이 있습니다. UITableViewDelegate의 tableView:didSelectRowAtIndexPath: 메서드 외부의 닿기를 처리하기 위해 UIButton 및 UIImageView 개체를 원합니다.UITableViewCell 내부에서 별도의 핸들 처리

@implementation CustomTableViewController 
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
     // handle cell selected 
    } 
@end 

내있는 UITableViewCell :은 buttonClicked 및 imageClicked 대신 컨트롤러의 didSelectRowAtIndexPath

@implementation CustomTableViewCell 

    - (id)init { 
     [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]; 

     [image addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked)]]; 
     [image setUserInteractionEnabled:YES]; 
    } 

    /* 
    * I want these to be called instead of tableView:didSelectRowAtIndexPath: 
    * when the button or image are tapped and I want 
    * tableView:didSelectRowAtIndexPath: to be called when any other part of 
    * the cell is tapped. 
    */ 

    - (void)buttonClicked { 
     // not called 
    } 

    - (void)imageClicked { 
     // not called 
    } 
@end 

를 호출 할 어떻게합니까?

답변

0

의 부동산에 관한 자세한 내용을보실 수 있습니다 우리는 다음과 같은 있었다 우리가 원한 것이지만 touchesCancelled에게 전화하는 것은 잘못된 것처럼 보입니다. 이 경우를 처리 할 수있는 더 좋은 방법이 있습니까?

0

당신이하고 싶은 것이 무엇인지 모르겠으므로 도움이 될 수도 있고 도움이되지 않을 수도 있지만, Delays Content Touches 속성이 storyboard/xib 파일에 있습니다. 이 속성은 기본적으로 하위 뷰보다 먼저 table/scrollview를 고려하지만 스크롤하기 전에 하위 뷰를 고려하면 선택 취소하면됩니다.

물론 이것은 스크롤하는 동안 버튼/이미지를 터치 할 수있는 것과 같은 다른 오류를 유발합니다.하지만 문제가되지 않는다면 이는 당신의 해결책 일 수 있습니다.

최상의 결과를 얻으려면 스토리 보드의 Cancellable Content Touches 속성이 YES (또는 선택됨)로 설정되어 있는지 확인하십시오. 기본적으로 버튼을 누르고 스크롤 뷰가 스크롤을 시작하고 손가락을 드래그하면 터치 이벤트를 취소하십시오.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    if (CGRectContainsPoint([self.button frame], [touch locationInView:self])){ 
     [self buttonClicked]; 
     /* Added this to resolve the issue 
     * [super touchesCancelled:touches withEvent:nil]; 
     */ 
    } 
    else if (CGRectContainsPoint([self.container frame], [touch locationInView:self])) { 
     [super touchesEnded:touches withEvent:event]; 
    } 
    else{ 
     [self imageClicked]; 
     /* Added this to resolve the issue 
     * [super touchesCancelled:touches withEvent:nil]; 
     */ 
    } 

} 

우리는 달성 한 것 같다 첫 번째와 세 번째 조건에 [슈퍼 touchesCancelled] 추가 :

당신은 CustomTableViewCell에서 UIScrollView Class Reference

관련 문제