2016-07-12 3 views
1

신속하게, 나는 두 번 탭과 한 번 탭이 구현 된 uitableviewCell을 사용합니다. 더블 탭이 작동합니다. 그러나 한 번의 탭으로 약간의 문제가 있습니다. 더블 탭의 현재로 인해, 나는 타이머가있는 싱글 탭을 구현했습니다. 다음 코드는 성공적으로 인쇄 은 scheduleTimerwithTimeInterval에 대한 selector에서 인수를 전달합니다.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    var now = NSDate().timeIntervalSince1970 

    if (now - lastClick < 0.3) && indexPath.isEqual(lastIndexPath) { 
     // Double tapped on cell 
     if let cell = discoverTableView.cellForRowAtIndexPath(indexPath) as? CommentCell { 
      cell.doubleTapCellActive({ (addBumpStatus, success) in 
      }) 
     } 
     singleTapTimer?.invalidate() 

    } else { 
     singleTapTimer = NSTimer.scheduledTimerWithTimeInterval(0.31, target: self, selector: #selector(DiscoverVC.singleTapped), userInfo: nil, repeats: false) 
    } 
    lastClick = now 
    lastIndexPath = indexPath 
} 

func singleTapped() { 
    print("Single tapped") 
} 

그러나, 문제는, 내가 하나의 탭이 선택 된 인덱스 경로를 알고 싶어 "단일 도청".

#selector(DiscoverVC.singleTapped(_:indexPath)) 
func singleTapped(indexPath: NSIndexPath) {} 

그러나 이것은 선택자가이 구문을 좋아하지 않으므로 오류가 발생합니다. 선택기를 작동시키는 방법이나 더 나은 방법을 만들 수있는 방법이 있습니까? 일반적인 방법은 NSTimer 매개 변수

func singleTapped(timer : NSTimer) { 
    print("Single tapped", timer.userInfo!["indexPath"] as! NSIndexPath) 
} 

와 동작을 구현하고, 예를 들어 타이머의 userInfo 속성을 통해 사용자 데이터를 전달하는 것입니다

+1

이유를 더블 클릭에 NSTimer를 사용하고 있습니까? 제스처도 사용할 수 있습니다. – ivarun

+0

테이블에있는 것은 모두 세 개의 레이블이며 셀에 제스처를 추가 할 수 있다고 생각하지 않습니까? 나는 세포 안의 모든 것에 몸짓을 더하고 싶지 않았다. – user172902

답변

1

감사합니다,

singleTapTimer = NSTimer.scheduledTimerWithTimeInterval(0.31, 
         target: self, 
         selector: #selector(singleTapped(_:)), 
         userInfo: ["indexPath":indexPath], 
         repeats: false) 
+0

갈 길이 멀다. 하지만 trie가 timer.userInfo를 검색 할 때! NSIndexPath, 일부 EXC_BAD_ACCESS (코드 = 1, 주소 = 0x18) 오류가 발생했습니다. 심지어 인수가 전달 될 수 있는지 확인하기 위해 selector를 "singleTapped :"로 변경했습니다. – user172902

+0

예제가 작동해야합니다. 'userInfo' 매개 변수를 사전으로 지정했습니다. – vadian

+0

작동 중입니다. 고마워. 나는 타이머가 무효화 된 후 userInfo를 얻으려고했다. 금지되어 있습니다. – user172902