當我把我的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
}
}
非常感謝沙沙。 –