简体   繁体   中英

Cannot set custom Long Press Gesture Recognizer on TableView

When I set the delegate of my CustomLongPressGestureRecognizer to the view controller, I get the following error

fatal error: unexpectedly found nil while unwrapping an Optional value

Following is the code:

 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.
   }
   }

The code of my CustomLongPressGestureRecognizer is:

 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

  }
 }

Remove weak here

weak var longPressGesture: CustomLongPressRecognizer!

It should be just

var longPressGesture: CustomLongPressRecognizer!

Edited:

Also it is not a good practice to create gesture recognizer in each cell. Try this:

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)")
}

您正在将委托设置为self ,但是您甚至没有实现UIGestureRecognizerDelegate一种方法,因此您只需删除导致崩溃的那一行即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM