当我将 CustomLongPressGestureRecognizer 的委托(delegate)设置为 View Controller 时,出现以下错误
fatal error :在解包可选值时意外发现 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
}
}
最佳答案
删除此处的弱项
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)")
}
关于ios - 无法在 TableView 上设置自定义长按手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35956015/