简体   繁体   English

如何在Swift 2中的另一个ViewController中滑动删除Core Data TableViewCell(带行动作)

[英]How to swipe to delete Core Data TableViewCell (with row action) in another ViewController in Swift 2

To start off I just want to say I am new to iOS Programming and this is my first app so I apologize if I use incorrect terminology or what not. 首先,我只是想说我是iOS编程新手,这是我的第一个应用程序,所以如果我使用不正确的术语或者不使用,我会道歉。

My app is set up so that I can add an event using Core Data and then 2 tableViews will update with information from that Event. 我的应用程序已设置为可以使用Core Data添加事件,然后2个tableView将使用该事件的信息进行更新。 The first tableView I have it configured so that I can swipe to delete the cell and the information from core data. 我配置了第一个tableView,以便我可以滑动以从核心数据中删除单元格和信息。

Swipe to Delete Code: 滑动即可删除代码:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    // Enables swipe to delete for cells

    if editingStyle == .Delete {

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let moc = appDelegate.managedObjectContext

        moc.deleteObject(events[indexPath.row])
        appDelegate.saveContext()

        events.removeAtIndex(indexPath.row)
        tableView.reloadData()
    }
}

In the other tableView however I have a UITableViewRowAction Edit button set up so that when clicked it will take the user to a EditViewController where they can edit the cell. 然而,在另一个tableView中我设置了一个UITableViewRowAction Edit按钮,这样当单击它时,它将把用户带到EditViewController,在那里他们可以编辑单元格。

UITableViewRowAction Code: UITableViewRowAction代码:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " Edit    ") { (action , indexPath ) -> Void in
        self.editing = false

        self.performSegueWithIdentifier("edit", sender: self)

        print("Edit button pressed")
    }

    return [editAction]

}

I want a button in this EditViewController to delete the cell and the Core Data information of the cell clicked like I did in the first tableView. 我希望在这个EditViewController中有一个按钮来删除单元格和单击的单元的核心数据信息,就像我在第一个tableView中所做的那样。 In the EditViewController I tried using similar code to the first tableView... 在EditViewController中,我尝试使用与第一个tableView类似的代码...

var events = [Events]()

@IBAction func deleteButton(sender: AnyObject) {

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let moc = appDelegate.managedObjectContext

    moc.deleteObject(events[indexPath.row])
    appDelegate.saveContext()

    events.removeAtIndex(indexPath.row)

    navigationController?.popViewControllerAnimated(true)

}

But this does not work because I cannot access the indexPath of the cell that I clicked on. 但这不起作用,因为我无法访问我单击的单元格的indexPath。 Is there anyway that when I click the UITableViewRowAction Edit Button that I can record the indexPath of the cell and use that information in the EditViewController? 无论如何,当我单击UITableViewRowAction编辑按钮时,我可以记录单元格的indexPath并在EditViewController中使用该信息?

Step one is to pass the relevant Event to the EditViewController. 第一步是将相关事件传递给EditViewController。 First, amend your edit action closure to get the relevant Event and pass it in the performSegueWithIdentifier call: 首先,修改您的编辑操作闭包以获取相关事件并在performSegueWithIdentifier调用中传递它:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: " Edit    ") { (action , indexPath ) -> Void in
        self.editing = false
        let eventToPass = events[indexPath.row]
        self.performSegueWithIdentifier("edit", sender: eventToPass)

        print("Edit button pressed")
    }
    return [editAction]    
}

Then implement prepareForSegue , which is called after you trigger the segue, but before the segue actually occurs. 然后实现prepareForSegue ,它在你触发segue之后但在segue实际发生之前被调用。 It's the place to pass information from the first view controller to the segue's destination view controller: 它是将信息从第一个视图控制器传递到segue的目标视图控制器的地方:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "edit" {
        let editVC = segue.destinationViewController as! EditViewController
        editVC.eventToEdit = sender as! Events
    }
}

You will need to add an eventToEdit property to your EditViewController : 您需要将一个eventToEdit属性添加到EditViewController

var eventToEdit : Events?

You should then be able to use this property to access the various attributes of the object, and your deleteButton code can just delete this object: 然后,您应该能够使用此属性来访问对象的各种属性,而deleteButton代码只能删除此对象:

@IBAction func deleteButton(sender: AnyObject) {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let moc = appDelegate.managedObjectContext
    moc.deleteObject(eventToEdit)
    appDelegate.saveContext()
    navigationController?.popViewControllerAnimated(true)
}

That should delete the object successfully, but your table view controller will be blissfully unaware. 这应该成功删除对象,但你的表视图控制器将是幸福不知道的。

Step two is to ensure that the TVC recognises that the object has been deleted. 第二步是确保TVC识别出该对象已被删除。 There are several ways to achieve this: 有几种方法可以实现这一目标:

  1. Add some code to viewWillAppear in the table view controller, which reloads the Events data from CoreData and reloads the table view. 在表视图控制器中向viewWillAppear添加一些代码,该控制器从CoreData重新加载Events数据并重新加载表视图。
  2. Replace the popViewController with an Unwind Segue. 用Unwind Segue替换popViewController
  3. Implement a protocol/delegate which enables the EditViewController to directly tell the TVC that the Event has been edited or deleted. 实现一个协议/委托,使EditViewController能够直接告诉TVC该事件已被编辑或删除。
  4. Change your TVC to use a NSFetchedResultsController , which will observe inserted/updated/deleted Events and automatically update the table view. 更改您的TVC以使用NSFetchedResultsController ,它将观察插入/更新/删除的事件并自动更新表视图。

I would begin by implementing the first of these, and then experiment with the others when you get more confident. 我首先要实现其中的第一个,然后在你更自信的时候尝试其他的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM