Refresh detail view when deleting table cell - Swift

I have a split view controller (master-detail). I will optimize it for iPhone 6 Plus.

Here's the problem. When you select a cell, it runs a show-detail segue and displays the cell information in a detail view. However, when you delete a cell, the detail view retains the cell information even if the cell no longer exists.

Here's an example:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {

    //code that deletes my cell/not needed for example

    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left)

    let splitController = self.splitViewController!.viewControllers
    self.detailViewController = splitController.last? as? detailedController
    if let detail = detailViewController {
        detail.title = "Select an item" 
        //detail.title is a variable in the view controller
        //on the views "viewDidLoad" it sets a label.text as title.
        }
    }
}

      

I want to perform the action: detail.title = "Select Item" so that the view no longer displays the deleted cell. I tried to make a fresh jump through the code. Bad luck.

Ideas?

+3


source to share


1 answer


Follow these steps:

  • Make sure your segment that defines the detail view controller has an ID. In the Master-Detail project example, the identifier "showDetail"

    . You can find the identifier by selecting segue in the plan view of the project document and then look in the identifier field in the attribute inspector for the segment.

  • Call this segue programmatically when you remove the line:

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Call showDetail when in a splitViewController and 2 view controllers
            // are present
            if self.splitViewController?.viewControllers.count == 2 {
               self.performSegueWithIdentifier("showDetail", sender: self)
            }
            ...
    
          

  • In prepareForSegue

    make sure that you have checked the selected row. When you call segue from the cell deletion code, no row will be selected, but indexPathForSelectedRow()

    will return nil

    :

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow() {
                // set up destination view controller in the normal way
                ...
            } else {
                // No row was selected which means either this is the initial setup
                // or we came here from the row delete code
                let splitController = self.splitViewController!.viewControllers
                self.detailViewController = splitController.last? as? detailedController
                if let detail = detailViewController {
                    detail.title = "Select an item"
                    ...
            }
        }
    }
    
          



Note. If the detail view controller has a default state if detail.title

not set, and that default state is what you want your destination view controller to look like, if no row is selected, then you don't even need a clause else

in prepareForSegue

. This applies to the iOS Master-Detail sample app. You only need to call the segue programmatically from your editing code and it just works.

+2


source







All Articles