当我将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
}
}
答案 0 :(得分:4)
删除弱点
video {
width: 100%;
height: auto;
}
应该只是
overflow:hidden
编辑:
在每个单元格中创建手势识别器也不是一个好习惯。试试这个:
weak var longPressGesture: CustomLongPressRecognizer!
答案 1 :(得分:0)
您正在将委托设置为self
,但您并未实施UIGestureRecognizerDelegate
中的一种方法,因此您只需删除导致崩溃的那一行。