Ios 无法在TableView上设置自定义长按手势识别器
当我将CustomLongPressGestureRecognitor的委托设置为视图控制器时,出现以下错误 致命错误:在展开可选值时意外发现nil 代码如下:Ios 无法在TableView上设置自定义长按手势识别器,ios,swift,uigesturerecognizer,Ios,Swift,Uigesturerecognizer,当我将CustomLongPressGestureRecognitor的委托设置为视图控制器时,出现以下错误 致命错误:在展开可选值时意外发现nil 代码如下: import UIKit class DevicesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate { weak var longPressGesture: Cust
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.
}
}
我的CustomLongPressGestureRecognitor的代码是:
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
}
}
您正在将委托设置为self,但您甚至没有从UIGestureRecognitzerDelegate中实现一个方法,因此您可以删除导致崩溃的那一行。此处删除弱
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)")
}
当我注释该行时,错误出现在下一行,即cell.addgestureRecognitizerLongPress手势;非常感谢你,沙沙。