2013-01-20 2 views
0

내가 정의 셀을 사용, 도청 된 세포 알고 이런 식으로 이미지가 체크 박스 (있는 UIImageView를) 정의 : 내가 사용의 TableView addGesture 내가 섹션에있는 tableView를 만들

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"cellIdentifier"; 
    StandardCellWithImage *cell = (StandardCellWithImage *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) { 
     cell = [[StandardCellWithImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSeparatorStyleNone; 
    } 

    cell.checkbox.tag = indexPath.row; 
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSelectedImageAtIndexPath:)]; 
    tapGesture.numberOfTapsRequired = 1; 
    tapGesture.delegate = self; 
    [cell.checkbox addGestureRecognizer:tapGesture]; 
    cell.checkbox.userInteractionEnabled = YES; 

    return cell; 
} 

그리고 didSelectedImageAtIndexPath의 방법 :

- (void) didSelectedImageAtIndexPath:(id) sender { 
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag inSection:0]; 
} 

하지만이 섹션에는 사용자가이 행을 탭하는 것에 대한 지식이 없어도 여기에있는 행만 있습니다. 그것을 인식 할 가능성이 있습니까?

답변

2

이 같은 view.tag 내 항목/섹션을 인코딩하는 경우에 대해 무엇 :

view.tag = indexPath.section * kMAX_SECTION_SIZE + indexPath.item; 

는 당신은 할 수 :

- (void) didSelectedImageAtIndexPath:(id) sender { 
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender; 


    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:gesture.view.tag % kMAX_SECTION_SIZE 
             inSection:int(gesture.view.tag/kMAX_SECTION_SIZE)]; 
} 
+0

멋진 트릭 세르지오! 감사 – edzio27

1

대신 체크 박스에 제스처를 추가 (셀 버튼)에서의 cellForRowAtIndexPath, 셀 (StandardCellWithImage) 자체에서 버튼 동작을 구현하고 거기에서 대리자를 호출합니다.

  1. 셀의 버튼에 조치를 취하십시오. & 자체적으로 구현하십시오.
  2. didSelectedImageAtIndexPath 당신이 원하는 그 안에 세포 & 선언 방식의 프로토콜을 선언 :
  3. 이 SELT하는 cellForRowAtIndexPath에서 뷰 컨트롤러 (뷰 컨트롤러) 세포의
  4. 설정 대리자를이 프로토콜을 구현
  5. 당신이 살짝 누르면 셀의 체크 박스 메소드가 호출되어 체크 박스 버튼에 대한 액션으로 설정됩니다.
  6. 거기에서 전화 대리자 메서드 didSelectedImageAtIndexPath :을 (를) 가져 왔습니다. 물론 [(UITableView *) self.superview indexPathForCell : self]를 사용하여 indexPath 객체를 반환 할 수 있습니다. // 여기에 자기 = custon 세포 개체

참고 :

당신은 당신이 -tableView에 설정했던 셀에있는 tableView에 약한 참조 저장할 수 있습니다 : cellForRowAtIndexPath을 : 테이블의는 dataSource의 . self.superview에 의존하는 것이 tableView가 항상 정확하기 때문에 이것은 더 낫다. 깨지기 쉽다. 미래에 UITableView의 뷰 계층 구조를 Apple이 어떻게 재구성 할 수 있는지 알고있는 사람.

관련 문제