【问题标题】:how to delete row from coredata (Entity) ios swift如何从coredata(实体)ios swift中删除行
【发布时间】:2016-09-30 06:39:37
【问题描述】:

我是我的应用程序中的 CORE DATA 的新手,我正在使用 coredata。 我只是将数据存储在我的核心数据中。我的实体名称是“FEED”,我有一个名称为“title”、“id”、“link”、“desc”的行,所以现在我想根据“id”删除特定的行。那么我该怎么做呢?

这是我的代码,

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


}

那我该怎么做呢?

【问题讨论】:

    标签: ios swift sqlite uitableview core-data


    【解决方案1】:

    像这样尝试,

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

    希望这会对你有所帮助。

    【讨论】:

    • let _ : NSError! = nil :')
    【解决方案2】:
    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")
    }
    

    【讨论】:

    • 让 index = sender.tag print(index) let feedId = person?.valueForKey("feed_id")?.objectAtIndex(index) deleteFeed(feedId! as! String)
    • 通过上面的代码我知道 Nfstring ObjectAtindex 不能使用
    • 那么我怎样才能在那里获得我的提要 ID?
    • let btnPostion = sender.convertPoint(CGPointZero, toView: tableView) let indexPath = self.tableView.indexPathForRowAtPoint(btnPostion) 你可以使用这两行来获取表格视图单元格的特定索引
    • 好吧,您的意思是说您面临如何为 tableview 获取选定索引路径的问题?只需在您的 buttonDeletePressed 函数中放入 2 行以下,您将获得所选行的索引路径 let btnPostion = sender.convertPoint(CGPointZero, toView: tableView) let indexPath = self.tableView.indexPathForRowAtPoint(btnPostion) print(indexpath.row) now fetch your feed id from array using like a let feedID = arrayOffeed[indexpath.row]as! String print(feedID) 试试这个
    【解决方案3】:

    您可以删除数据,例如,

      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 并像上面提到的那样删除数据。

    希望这会有所帮助:)

    【讨论】:

      【解决方案4】:
      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
          }
      }
      

      希望这能有所帮助!

      【讨论】:

        猜你喜欢
        • 2017-04-24
        • 1970-01-01
        • 1970-01-01
        • 2014-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-14
        相关资源
        最近更新 更多