2016-03-12 39 views
2

當我把我的CustomLongPressGestureRecognizer的委託視圖控制器,我得到以下錯誤無法設置自定義的長按手勢識別器上的TableView

致命錯誤:意外發現零而展開的可選值

以下是代碼:

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 

    } 
} 

回答

4

刪除弱這裏

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

非常感謝沙沙。 –

0

您正在設置爲self的代理,但是您沒有實現從UIGestureRecognizerDelegate開始的一個方法,因此您可以刪除導致崩潰的那一行。

+0

當我評論該行時,錯誤出現在下一行,即cell.addGestureRecognizer(longPressGesture); –