2016-06-17 2 views
0

사용자 지정 테이블보기 셀을 사용하고 있습니다. 이 셀에는 chatNameLabel이라는 콘센트가있는 레이블이 있습니다. 이 레이블에 UILongPressGestureRecognizer를 추가하면 연결된 이벤트가 실행되지 않습니다.UITapGestureRecognizer를 테이블 뷰 셀 안에있는 UILabel에 어떻게 추가 할 수 있습니까? Swift2

UILabel이 TableView에 있고 테이블/셀보기가 탭을 가로 채고 있다는 것이 문제인 것 같습니다. 이것에 대해 뭔가 할 수 있을까요?

내가하고 싶은 것은 UILabel을 길게 누르면 일부 사용자 지정 작업을 수행하는 것입니다.

나는 이것이 Objective-C에서 답변을 얻었지만 나는 언어에 익숙하지 않고 신속하다는 점에 익숙하지 않다고 생각한다.

여기 내가 사용 코드입니다 :

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if let sessions = self.chatSessions where sessions.indices.contains(indexPath.row) { 
     let session = sessions[indexPath.row] 



     if session.sessionId == nil { 
      //DO A THING 
     } 
     // existing session id (existing chat) 
     else { 
      let cell = tableView.dequeueReusableCellWithIdentifier("ChatListCell", forIndexPath: indexPath) as! ChatListTableViewCell 
      cell.tag = indexPath.row 

      if(session.unreadChats) { 
       cell.indicatorImageView.tintColor = AppStyles.sharedInstance.indicatorActive 
      } 
      else{ 
       cell.indicatorImageView.hidden = true 
      } 
      //Want to do gesture on this label below cell.chatNameLabel 
      cell.chatNameLabel.text = session.chatName 

... 당신의 클래스 요구 UIGestureRecognizerDelegate 구현이

답변

2

아래 질문에 필요하지 않은 일부 더 많은 코드, 다음 아래의 코드는 작동합니다.

myLabel.userInteractionEnabled = true 
let tap: UILongPressGestureRecognizer = UILongPressGestureRecognizer(
target: self, action: #selector(tappedTheLabel)) 
tap.minimumPressDuration = 0.5 
tap.delaysTouchesBegan = true 
myLabel.addGestureRecognizer(tap) 
tap.delegate = self 
} 

func tappedTheLabel(sender: UITapGestureRecognizer) 
{ 
print("label hit \(sender)") 
}