2016-06-01 114 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 
    } 
} 

希望這可以有一些幫助!