2015-07-12 63 views
-1

我有一個自定義的UITableView單元格,我想爲它添加一個長按手勢識別器。目前,我做它像這樣:將LongPressGestureRecognizer添加到UITableViewCell

longPressGesture.minimumPressDuration = 1.0 
    longPressGesture.addTarget(self, action: "testFeedback") 
    cell.addGestureRecognizer(longPressGesture) 

我編程這麼做,是因爲我找不到,以檢測電池的IBAction爲內挖一個好辦法。然而,我很難得到這個工作,我想通過選擇器傳遞參數。我不反對在故事板中這樣做,但希望對此有所指導。

謝謝!

回答

1

testFeedback功能應該是這樣的

func testFeedback(gestureRecognizer:UIGestureRecognizer) { 

    if (gestureRecognizer.state == UIGestureRecognizerState.Ended) { 
     var point = gestureRecognizer.locationInView(self.tableView) 
     if let indexPath = self.tableView.indexPathForRowAtPoint(point) 
     { 
      println(indexPath.row) /// long press ended 
     } 
    } 
    else if (gestureRecognizer.state == UIGestureRecognizerState.Began){ 
      /// long press started 
    } 
} 
+0

謝謝!我會接受答案,當它讓我 –

+0

高興工作,快樂的迅速編碼 –