无法在 TableView 上设置自定义长按手势识别器
Cannot set custom Long Press Gesture Recognizer on TableView
当我将 CustomLongPressGestureRecognizer 的委托设置为视图控制器时,出现以下错误
致命错误:在展开可选值时意外发现 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
}
}
您正在将委托设置为 self
,但您甚至没有实现 UIGestureRecognizerDelegate
中的一个方法,因此您可以删除导致崩溃的那一行。
移除弱点
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)")
}
当我将 CustomLongPressGestureRecognizer 的委托设置为视图控制器时,出现以下错误
致命错误:在展开可选值时意外发现 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
}
}
您正在将委托设置为 self
,但您甚至没有实现 UIGestureRecognizerDelegate
中的一个方法,因此您可以删除导致崩溃的那一行。
移除弱点
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)")
}