将UILongPressGestureRecognizer添加到上一个tableview单元无法检测到locationInView

时间:2015-02-12 16:31:25

标签: ios iphone swift ios8.1

我正在尝试将UILongPressGestureRecongnizer添加到每个tableview单元格。以下代码适用于除最后一个单元格以外的所有情况:

<强烈> viewDidLoad中:

let longpress = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
longpress.minimumPressDuration = 0.35
tableView.addGestureRecognizer(longpress)

handleLongPress:

func handleLongPress(sender:UILongPressGestureRecognizer!) {
    let localLongPress = sender as UILongPressGestureRecognizer
    var locationInView = localLongPress.locationInView(tableView)

    // returns nil in the case of last cell
    // but strangely only on Ended State
    var indexPath = tableView.indexPathForRowAtPoint(locationInView)
    if indexPath != nil {
        var cell = self.tableView.cellForRowAtIndexPath(indexPath!) as MyCellTableViewCell
        println("Long press Block .................");     
        if (sender.state == UIGestureRecognizerState.Ended) {
            println("Long press Ended");
        } else if (sender.state == UIGestureRecognizerState.Began) {
            println("Long press detected."); 
        }
    } 
}

对于我的上一个单元格,我总是看到UIGestureRecognizerState.Began,但我看不到UIGestureRecognizerState.Ended。我看到每个单元格的状态,除了最后一个tableviewcell。在这种情况下,tableview.indexPathForRowAtPoint()解析为零。我究竟做错了什么?

0 个答案:

没有答案