当前位置: 首页 > 面试题库 >

从Coredata Swift删除数据

夏经武
2023-03-14
问题内容

在我的tableViewController中,我有以下内容。我正在尝试删除一个项目。

var myData: Array<AnyObject> = []

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellID: NSString = "Cell"
    var Cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell
    var data: NSManagedObject = myData[indexPath.row] as NSManagedObject
    Cell.textLabel?.text = data.valueForKeyPath("Name") as? String

    return Cell
}

然后尝试删除我有。

override func tableView(tableView: (UITableView!), commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        let cellID: NSString = "Cell"
        var Cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell
        var data: NSManagedObject = myData[indexPath.row] as NSManagedObject
        data.delete(0)

        // 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
    }
}

问题答案:

通过在swift和coredata中执行数据删除来更新我的编码问题。这最终使我的代码起作用了。

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        switch editingStyle {
        case .Delete:
            // remove the deleted item from the model
            let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
            let context:NSManagedObjectContext = appDel.managedObjectContext!
            context.deleteObject(myData[indexPath.row] as NSManagedObject)
            myData.removeAtIndex(indexPath.row)
            context.save(nil)

           //tableView.reloadData()
            // remove the deleted item from the `UITableView`
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        default:
            return

        }
}

上面针对Swift 2.2和Xcode 7.3.1的编辑

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    switch editingStyle {
    case .Delete:
        // remove the deleted item from the model
        let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context:NSManagedObjectContext = appDel.managedObjectContext
        context.deleteObject(myData[indexPath.row] )
        myData.removeAtIndex(indexPath.row)
        do {
            try context.save()
        } catch _ {
        }

        // remove the deleted item from the `UITableView`
        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    default:
        return
    }
}

还需要对这两行代码进行更正。

    var myData: Array<AnyObject> = []
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext


 类似资料:
  • 问题内容: 我是ElasticSearch的新手。我试图弄清楚如何从ElasticSearch中删除数据。我已删除索引。但是,这似乎并没有真正删除数据本身。我看到的其他内容指向“ 按查询删除”功能。但是,我什至不知道要查询什么。我知道我的索引。本质上,我想弄清楚如何做 来自Chrome中的PostMan。但是,我没有任何运气。看来,无论我做什么,数据都会徘徊。到目前为止,我已经通过在PostMan

  • 我是JSP的新手,我正在尝试创建一个web界面,用户可以在该界面中输入他们想要删除的信息,并且该信息将在数据库表中删除。 在这里,他们应该输入和,然后应该删除具有两个指定ID的任何数据。但是,它不是从表中删除的。我有个例外 数组索引越界 下面是我的代码:

  • 问题内容: 这是我在角度控制器中创建的数组。 然后,我使用push方法将一些值插入到数组中: 这是插入后的数组值: 在插入之后,这里已经向数组添加了额外的东西,但是我需要从数组中删除它。 问题答案: 根据您需要将数组插入数据库的评论,我将假设您将其转换为JSON字符串,然后将其保存到数据库中。如果不正确,请告诉我,我将看看是否可以修改此答案。 在将数组转换为JSON时,有两个选项可用于修改数组。第

  • 我刚加入弹性搜索公司。我正在想办法从ElasticSearch中删除数据。我已经删除了我的索引。然而,这似乎并没有真正删除数据本身。我看到的其他内容指向Delete by Query特性。然而,我甚至不确定该查询什么。我知道我的索引。从本质上说,我想弄清楚如何做一个 来自Chrome版邮递员。但是,我没有什么运气。好像不管我做什么,数据都挂在那里。到目前为止,我已经通过使用PostMan中的DEL

  • 我正试图从数据库中删除一行,但得到以下错误

  • 我有一个包含大量列的Spark数据框架。我想从中删除两列以获得新的数据帧。 如果列更少,我可以在API中使用select方法,如下所示: 但是既然从长列表中挑选列是一项乏味的任务,有解决方法吗?