【问题标题】:delete tableViewCell on long press长按删除tableViewCell
【发布时间】:2016-05-08 15:47:55
【问题描述】:

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

这是我用于在 cellForRowAtIndexPath 函数中设置手势识别器的代码:

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。感谢您花时间阅读本文并可能提供答案。我希望我没有在某个地方犯过新手错误。

【问题讨论】:

  • 您发布的代码显示的具体问题是什么?问题是什么?
  • 我发布的代码给了我错误“无法识别的选择器发送到实例......”我不知道为什么它给了我这个错误,所以我想知道我的代码是否已经发布的可以修复以防止错误。另外,如果有一种“正确”的方式来做这件事,那么我也想知道这个过程。

标签: ios swift uitableview uigesturerecognizer


【解决方案1】:

不正确的选择器

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

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

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

func longPressDelete() { }

【讨论】:

    【解决方案2】:

    非常简单的例子,如果你想在 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 //
    
     }       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2012-02-28
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多