简体   繁体   中英

What exactly does setEditing() do in Swift?

I am implementing navigationItem.leftBarButtonItem = editButtonItem under viewDidLoad() , and it is said that I have to implement setEditing(_ editing: Bool, animated: Bool) as well. It seems like every editing functionality works great without setEditing function. What does it do??

  override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.prefersLargeTitles = true
    navigationItem.leftBarButtonItem = editButtonItem
    tableView.allowsMultipleSelectionDuringEditing = true
  }

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: true)
        tableView.setEditing(tableView.isEditing, animated: true)
    }

and it is said that I have to implement setEditing(_ editing: Bool, animated: Bool) as well

Then "it is said" incorrectly.

The built-in editButtonItem of a UITableViewController automatically calls the table view's setEditing for you; there is no need, therefore, to duplicate that functionality. To be more precise:

  1. The built-in editButtonItem of a UIViewController does two things:

    • It calls the UIViewController's setEditing(_:animated:) when tapped.

    • It tracks the UIViewController's isEditing property, and changes its own title accordingly (Edit or Done).

  2. Moreover, UITableViewController's implementation of setEditing(_:animated:) calls setEditing(_:animated:) on its table view.

Thus, you would need to do that last step if this were not a UITableViewController. But it is, so you don't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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