ios - 自定义的 EditAction 按钮不触发事件

标签 ios swift

我在导航栏中有一个带有编辑按钮的 tableviewcontroller 这已被覆盖以显示选项和完成而不是编辑和完成..

在我的 viewDidLoad 函数中我有...

    // Display an Edit button in the navigation bar for this view controller.
    self.navigationItem.leftBarButtonItem = self.editButtonItem()
    // change initial text from Edit to Options
    self.navigationItem.leftBarButtonItem!.title = "Options"

我还有以下...

override func setEditing (editing:Bool, animated:Bool)
{
    //set different text for Edit and Done
    super.setEditing(editing,animated:animated)
    if(self.editing)
    {
        self.editButtonItem().title = "Done"
    }else
    {
        self.editButtonItem().title = "Options"
    }
}

为了完整起见,这是我的编辑操作代码。

// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    switch editingStyle {

    case .Delete:
        let context = fetchedResultsController.managedObjectContext
        if indexPath.row == 0 {
            //this is the top (first row)
            // Deleting without warning
            let indexPathToDelete = NSIndexPath(forRow: 0, inSection: 0)
            let objectToDelete = fetchedResultsController.objectAtIndexPath(indexPathToDelete) as! NSManagedObject
            context.deleteObject(objectToDelete)

            do {
                try context.save()
            } catch {
                print(error)
            }

        } else {
            //we are deleted a row that is not the top row
            // we need to give a warning and if acknowledged then delele all rows from the selected row and all rows above it

            let alertController = UIAlertController(title: nil, message: "Are you sure? This will remove this and all logs above it.", preferredStyle: .Alert)
            let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in

            }
            alertController.addAction(cancelAction)
            let deleteAction = UIAlertAction(title: "Delete", style: .Default) { (action) in

                for deleteindex in 0 ... indexPath.row {
                    let deleteIndexPath = NSIndexPath(forRow: deleteindex, inSection: 0)
                    let objectToDelete = self.fetchedResultsController.objectAtIndexPath(deleteIndexPath) as! NSManagedObject
                    context.deleteObject(objectToDelete)
                }

                do {
                    try context.save()

                } catch {
                    print(error)
                }
            }
            alertController.addAction(deleteAction)

            // Dispatch on the main thread
            dispatch_async(dispatch_get_main_queue()) {
                self.presentViewController(alertController, animated: true, completion:nil)
            }

        }
        break;

    default :
        return
    }

}

这很好用...

现在我已经添加了自定义编辑操作...通过添加此代码通常的删除 + 导出...

//Override edit actions
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let delete = UITableViewRowAction(style: .Destructive, title: "Delete") { (action, indexPath) in
        // delete item at indexPath
    }

    let export = UITableViewRowAction(style: .Normal, title: "Export") { (action, indexPath) in
        // export item at indexPath
    }

    export.backgroundColor = UIColor.blueColor()

    return [delete, export]
}

现在,当处于编辑模式时,每个单元格现在都有两个选项“导出”和“删除”,但按下时都不执行任何操作。

我在行 case 处有一个断点。删除但它没有被击中。

最佳答案

您必须将删除代码放入其操作中:

// Override edit actions
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let delete = UITableViewRowAction(style: .Destructive, title: "Delete") { (action, indexPath) in
        // You code for Deletion here... 

    }

关于ios - 自定义的 EditAction 按钮不触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37260495/

相关文章:

ios - 是否可以更改 ios 的 snackbar 的背景颜色?

ios - AlertController 的代码不正确

swift - Eureka rangeSliderRow

ios - 从应用程序内部触发推送通知

ios - Firebase:当用户终止应用程序时如何从 Firebase 中清除特定数据?

ios - 是否有可能在 iOS 中获得孟加拉语的文本到语音?

ios - UISearchBar resignFirstResponder 不工作

ios - 为什么在使用 Codemagic 为 Flutter 构建代码时会收到 "Failed to build for iOS"?

ios - 键盘来时,如何设置UITextView中的文本字段上下移动?

ios - 带有颜色渐变的 UIBezierPath