ios - 无法在 TableView 上设置自定义长按手势识别器

标签 ios swift uigesturerecognizer

当我将 CustomLongPressGestureRecognizer 的委托(delegate)设置为 View Controller 时,出现以下错误

fatal error :在解包可选值时意外发现 nil

代码如下:

 import UIKit

class DevicesViewController: UIViewController, UITableViewDataSource,  UITableViewDelegate, UIGestureRecognizerDelegate {

weak var longPressGesture: CustomLongPressRecognizer!

@IBOutlet weak var deviceView: UITableView!

@IBOutlet weak var correspondingUserView: UITableView!

var devices=[String]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    devices.append("BBIPad")


}

internal func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return devices.count
}

internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    if (tableView.isEqual(deviceView)){

        let cell = UITableViewCell();
        cell.textLabel!.text = devices[indexPath.row]
        longPressGesture = CustomLongPressRecognizer(target: self, action: Selector("handleLongPress:"), index: indexPath.row);

        //In Below Line I get the crash
        longPressGesture.delegate = self

        cell.addGestureRecognizer(longPressGesture);
        return cell
    }
    else{

        let cell = UITableViewCell();
        cell.textLabel!.text = "Shubham"
        longPressGesture = CustomLongPressRecognizer(target: self, action: Selector("handleLongPress:"), index: indexPath.row);

        //In Below Line I get the crash
        longPressGesture.delegate = self

        cell.addGestureRecognizer(longPressGesture);

        return cell
    }
        func tableView(tableView: UITableView,                       didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if (tableView.isEqual(deviceView)){
        //Program to get user for corresponding device

    }
    else{
        //Program to get device for corresponding user
    }
}

func handleLongPress(recogniser :CustomLongPressRecognizer!){
    NSLog("The indexpath: ", recogniser.index)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
   }
   }

我的 CustomLongPressGestureRecognizer 的代码是:

 import UIKit.UIGestureRecognizerSubclass

 class CustomLongPressRecognizer: UILongPressGestureRecognizer {
internal var index: NSInteger!
   init(target: AnyObject?, action: Selector, index: NSInteger) {
    super.init(target: target, action: action)
        self.index = index

  }
 }

最佳答案

删除此处的弱项

weak var longPressGesture: CustomLongPressRecognizer!

应该是这样

var longPressGesture: CustomLongPressRecognizer!

编辑:

此外,在每个单元格中创建手势识别器也不是一个好习惯。试试这个:

var longPressRecognizer: UILongPressGestureRecognizer!
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
    tableView.addGestureRecognizer(longPressRecognizer)
}

func handleLongPress(recognizer :UILongPressGestureRecognizer!){
    let touchPoint = recognizer.locationInView(tableView)
    let indexPath = tableView.indexPathForRowAtPoint(touchPoint)
    print("\(indexPath!.row)")
}

关于ios - 无法在 TableView 上设置自定义长按手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35956015/

相关文章:

ios - Windows Media Center的iPhone Remote 的任何源代码?

ios - 嵌套 UITableView 不返回任何单元格

IOS - 在滚动时将 UIView 保持在 UITableView 之上

json - 解析单个 JSON 元素时出现问题 (Swift)

ios - UIControlEventTouchUpInside 在 UIPanGesture 之后没有触发

c# - 推送通知无法使用 asp.net 到达 ios 设备?

swift - 如何在 Swift 中更改 TableView 单元格中 UIButton 的颜色?

ios - 持久化数据并传递给多个 View

ios - 如何找到特定帧大小的所有 subview ?

iphone - 使用 UIImageWriteToSavedPhotosAlbum 保存图像时无法识别的选择器错误