"Swift Core Data" 和 UISearchController

"Swift Core Data" and UISearchController

我有一个 tableViewNSFetchedResultsController!我已经完全完成了 NSFetchedResultsController 的委托方法并且工作完美!我的问题行显示 UIAlertControllerUIAlertControllertableView 上运行良好,但在 UISearchController 内不起作用。我试图删除 UISearchController 内的对象。当我按下删除按钮时 Xcode 给我这样的错误:

这是我的 commitEditingStyle 方法和 UIAlertControllerUIAlertActionhandler:

的代码

`// 覆盖以支持编辑 table 视图。 override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 如果 editingStyle == .Delete {

        let itemToDelete:Manager = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Manager
        prepareForDelete(itemToDelete)

        // Delete the row from the data source
        //tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}

// Delete Action
var itemsToDelete:Manager!

// Delete function

私有函数 prepareForDelete(managedObject:Manager) {

    //
    self.itemsToDelete = managedObject

    // Alert
    let alert:UIAlertController = UIAlertController(title: "Warning!", message: "Do you want to delete this note?", preferredStyle: UIAlertControllerStyle.Alert)

    // Actions
    let deleteAction:UIAlertAction = UIAlertAction(title: "Delete", style: UIAlertActionStyle.Destructive, handler: deleteHandler)

    // Actions
    let cancelAction:UIAlertAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

    // Add actions to the alert
    alert.addAction(deleteAction)
    alert.addAction(cancelAction)

    // Present alert
    self.presentViewController(alert, animated: true, completion: nil)
}

func deleteHandler(alert:UIAlertAction) -> Void {

    // Delete from the moc
    if let delete = self.itemsToDelete {
        self.managedObjectContext.deleteObject(delete)
        do {
            // Save changes
            try self.managedObjectContext.save()
        } catch {

        }
        self.itemsToDelete = nil
    }
}`

如何禁用UIAlertController?我不需要里面的警报 UISearchController。因为这个函数在 UISearchController

内部不起作用

感谢您的关注!

您可以检查您的搜索控制器是否处于活动状态(我假设您在视图控制器中引用了您的搜索控制器)。

将此添加到 prepareForDelete 的开头:

guard !searchController.active else { return }

该代码检查搜索控制器是否未激活,如果是,则不执行任何代码。