长按

时间:2016-01-31 01:44:53

标签: ios swift uitableview uigesturerecognizer

我目前在使用Swift 2编写的Xcode 7.2上运行的iOS 9.2应用程序中有一个侧边栏菜单,允许用户加载哪些数据来填充视图。我使用SWRevealViewController创建该侧边栏。我有一个容器视图控制器,它有头版和侧边栏页面,列出了用户拥有的所有选项。每次用户从侧边栏表中选择一个单元格时,它都会执行一个允许刷新首页的segue。我想要做的是允许用户长按删除表格中的单元格。我想展示一个AlertViewController以确认用户的决定,以及是否"是"如果选中,我想删除单元格,并选择表格中的第一个项目。我已尝试按照Long press on UITableView的说明进行操作 但我不断收到错误"无法识别的选择器发送到实例"

以下是我用于在cellForRowAtIndexPath函数中设置gestureRecognizer的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell;
    cell.textLabel!.text = tableArray[indexPath.row];
    let holdToDelete = UILongPressGestureRecognizer(target: cell, action: "longPressDelete");
    holdToDelete.minimumPressDuration = 1.00;
    cell.addGestureRecognizer(holdToDelete);
    return cell;
}

这里是longPressDelete函数:

func longPressDelete(sender: UILongPressGestureRecognizer) {
    let alert: UIAlertController = UIAlertController(title: "Please Confirm", message: "Are you sure you want to delete this car from your database?", preferredStyle: .Alert);
    alert.addAction(UIAlertAction(title: "Yes", style: .Destructive, handler: { (UIAlertAction) -> Void in
        if let tv = self.tableView {
            let point: CGPoint = sender.locationInView(self.tableView);
            let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(point)!;
            tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade);
            NSUserDefaults.standardUserDefaults().removeObjectForKey("fillUp" + tableArray[indexPath.row]);
            NSUserDefaults.standardUserDefaults().removeObjectForKey("services" + tableArray[indexPath.row]);
            tableArray.removeAtIndex(indexPath.row);
            NSUserDefaults.standardUserDefaults().setObject(tableArray, forKey: "cars");
            self.deleted = true;
            self.performSegueWithIdentifier("tableToDashboard", sender: self);

        }
    }));
    alert.addAction(UIAlertAction(title: "No", style: .Default, handler: nil));
    self.presentViewController(alert, animated: true, completion: nil);
}

这是prepareForSegue函数:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (deleted) {
        let indexPath: NSIndexPath = NSIndexPath(forRow: 0, inSection: 0);
        fillUpKey = "fillUp" + tableArray[indexPath.row];
        servicesKey = "services" + tableArray[indexPath.row];
        localFillUpArray = [fillUp]();
    } else {
        let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow!;
        fillUpKey = "fillUp" + tableArray[indexPath.row];
        servicesKey = "services" + tableArray[indexPath.row];
        localFillUpArray = [fillUp]();
    }
}

我想要发生的是用户删除单元格中的项目,然后应用程序在从其他来源加载信息后执行到前面屏幕的segue。感谢您花时间阅读本文并提供答案。我希望我在某个地方没有犯过新的错误。

2 个答案:

答案 0 :(得分:1)

选择器不正确

let holdToDelete = UILongPressGestureRecognizer(target: self,
                                                action: "longPressDelete:");
    :表示方法longPressDelete实际接受参数后,
  • func longPressDelete(sender: UILongPressGestureRecognizer)

  • self target,假设选择器属于注册它的同一个类。

当前选择器"longPressDelete"将匹配不带参数的方法签名:

func longPressDelete() { }

答案 1 :(得分:0)

非常简单的示例,如果您想在uitableview中选择单元格

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longTap))
cell.addGestureRecognizer(longGesture)


 // longTap

 func longTap(gestureReconizer: UILongPressGestureRecognizer) {

    print("Long tap")

    let longPress = gestureReconizer as UILongPressGestureRecognizer
    _ = longPress.state
    let locationInView = longPress.location(in: tableview)
    let indexPath = tableview.indexPathForRow(at: locationInView)
 // whatever you want with indexPath use it //

 }