2016-06-01 131 views
0

我是新来的核心数据在我的应用程序中我正在使用coredata。 我刚刚将数据存储在我的核心数据中。我的实体名称是“FEED”,并且我有一个名为“title”,“id”,“link”,“desc”的行,所以现在我想根据“id”删除特定的行。所以我怎么做到这一点?如何删除coredata(实体)行ios swift

这里是我的代码,

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 


} 

,所以我怎么能做到这一点?

回答

3

尝试这样,

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)") 
     } 
} 

希望这会帮助你。

+0

其工作感谢 –

+0

不客气。 –

0

您可以像删除数据,

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) 

这是从commitEditingStyle这样做这里使用indexPath.row。如果你想从其他地方删除数据,你可以传递你的ID。如果你想从tableview中删除数据,那么你应该执行commitEditingStyle并删除上面提到的数据。

希望这将有助于:)

1
func deleteFeed(id:String) 
{ 
    let appDelegate = 
     UIApplication.sharedApplication().delegate as? AppDelegate 
    let managedContext = appDelegate?.managedObjectContext 
    let fetchRequest = NSFetchRequest(entityName:"FEED") 
    fetchRequest.predicate = NSPredicate(format: "id = %@", "\(id)") 
    do 
    { 
     let fetchedResults = try managedContext!.executeFetchRequest(fetchRequest) as? [NSManagedObject] 

     for entity in fetchedResults! { 

      managedContext?.deleteObject(entity) 
     } 
    } 
    catch _ { 
     print("Could not delete") 

    } 
} 

现在你可以调用这个函数并传递任何你想删除

func buttonDeletePressed(sender:UIButton){ deleteFeed("1") }

请注明我的答案,如果它可以帮助你的ID。

+0

令指数= sender.tag 打印(指数) 让feedId =人?.valueForKey( “FEED_ID”)?objectAtIndex(指数) deleteFeed(feedId!是!字符串) –

+0

通过上面的代码我得到Nfstring ObjectAtindex不能使用 –

+0

那么我怎样才能得到我的饲料ID呢? –

0
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(people[indexPath.row]) 
    people.removeAtIndex(indexPath.row) 
    do { 
     try context.save() 
    } catch _ { 
    } 

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

希望这可以有一些帮助!