본문 바로가기

iPhone

ios UITableView에서 Cell에 롱터치이벤트(UILongPressGestureRecognizer)를 주었을 때 몇 번째 Cell의 index값을 가져오는 방법

UITableView에 UILongPressGestureRecognizer를 등록 후 해당 제스처의 포지션을 가져와 리스트의 인덱스 포인터를 뽑아오면 됩니다.


테이블 뷰 제스처 등록 

//mailList UITableView 입니다.

   UILongPressGestureRecognizer* longClickEvent = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longClickCell:)];

    [mailList addGestureRecognizer:longClickEvent];

    [longClickEvent release];


제스쳐 핸들러 부분 

- (void)longClickCell:(UILongPressGestureRecognizer *)sender

{

    //제스처 종료 시점에 실행

    if (sender.state == UIGestureRecognizerStateEnded){

       // 해당 뷰의 선택된 영역의 CGPoint를 가져온다.

        CGPoint currentTouchPosition = [sender locationInView:[sender view]];

       // 테이블 뷰의 위치의 Cell의 indexPath를 가져온다 

       NSIndexPath *indexPath = [mailList indexPathForRowAtPoint:currentTouchPosition];

           

        NSLog(@"포지션 %i",indexPath.row);

        //원하는 부분의 정보를 변경 후

        Info *info = [ArrayList objectAtIndex:indexPath.row];

        info.longClickCheck = YES;

        //리스트뷰를 리로드 하면 변경된 화면을볼 수 있다.

        [mailList reloadData];

    }   

}