如何从coredata(实体)删除行ios swift [英] how to delete row from coredata (Entity) ios swift

查看:242
本文介绍了如何从coredata(实体)删除行ios swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的CORE数据在我的应用程序,我使用coredata。
我只是存储在我的核心数据的数据。我的实体名称是FEED,我有一个名为title,id,链接,desc的行,所以现在我想根据id删除perticular行。所以我怎么能这样做?

i am new to CORE DATA in my app i am using coredata. i just stored data in my core data. my entity name is "FEED" and i have a rows with name "title", "id " , "link" ,"desc" so now i want to delete perticular row on based on "id". so how can i do this?

这是我的代码,

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell : MessageMusicCell = tableView.dequeueReusableCellWithIdentifier("MessageMusicCell") as! MessageMusicCell

    cell.pinButton.tag = indexPath.row
    cell.pinButton.addTarget(self, action: #selector(MessageViewController.buttonDeletePressed(_:)), forControlEvents: UIControlEvents.TouchUpInside)
    cell.selectionStyle = .None



        person = people[indexPath.row]
        cell.lbl_title_msg_music.text = person!.valueForKey("title") as? String
        cell.img_message_music.image = UIImage(named: "PlaceHolder")
        cell.desc_msg_music.text = person!.valueForKey("desc") as? String


    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        print(people.count)
        return people.count


}

func buttonDeletePressed(sender:UIButton) {


}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cell : MessageMusicCell = tableView.dequeueReusableCellWithIdentifier("MessageMusicCell") as! MessageMusicCell


}

推荐答案

尝试这样,

func buttonDeletePressed(sender:UIButton) {

        let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context:NSManagedObjectContext = appDel.managedObjectContext

        let index = sender.tag

        context.deleteObject(people[index] as NSManagedObject)
        people.removeAtIndex(index)

        let _ : NSError! = nil
        do {
            try context.save()
            self.tableView.reloadData()
        } catch {
            print("error : \(error)")
        }
}

希望这会帮助你。

这篇关于如何从coredata(实体)删除行ios swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆