将表视图设置为编辑模式

时间:2022-10-26 22:18:56

I have a UITableView in a UIViewController and have added an edit button from code rather than IB. This comes with UITableViewControllers but not UIVCs. How can I get this button to put the table view into editing mode in swift? Thanks in advance for any help.

我在UIViewController中有一个UITableView,并且从代码而不是IB添加了一个编辑按钮。这附带UITableViewControllers但不是UIVC。如何在swift中使用此按钮将表格视图置于编辑模式?在此先感谢您的帮助。

class WordsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate  {
  override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.rightBarButtonItem = self.editButtonItem()
  }

  func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
  }
}

6 个解决方案

#1


12  

Create rightBarButtonItem as below with an action.

使用操作创建如下所示的rightBarButtonItem。

In viewDidLoad() :

在viewDidLoad()中:

var rightButton = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("showEditing:"))
    self.navigationItem.rightBarButtonItem = rightButton

and then make a function like,

然后做一个像,

 func showEditing(sender: UIBarButtonItem)
 {
    if(self.tableView.editing == true)
    {
        self.tableView.editing = false
        self.navigationItem.rightBarButtonItem?.title = "Done"   
    }
    else
    {
        self.tableView.editing = true
        self.navigationItem.rightBarButtonItem?.title = "Edit" 
    }
}

Make sure, : is appended to function name in Selector of action in viewDidLoad
Hope it helps!

确保,:在viewDidLoad中的操作选择器中附加到函数名称希望它有所帮助!

#2


7  

Call this method on button click.

按下按钮调用此方法。

tableView.setEditing(true, animated: true)

Or if you want it to work like a toggle use

或者,如果您希望它像切换使用一样工作

tableView.setEditing(!tableView.editing, animated: true)

I assume you have a button, which calls editButtonPressed on press. So implementation of this method could look like this.

我假设你有一个按钮,在按下时调用editButtonPressed。因此,此方法的实现可能如下所示。

override func viewDidLoad(){
 navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editButtonPressed"))
}

func editButtonPressed(){
  tableView.setEditing(!tableView.editing, animated: true) 
  if tableView.editing == true{
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editButtonPressed"))
  }else{
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editButtonPressed"))
  }
} 

This also changes title of the bar button.

这也改变了按钮的标题。

#3


7  

Swift 3 & 4 answer that IMHO is better than other answers:

Swift 3&4回答恕我直言比其他答案更好:

override func viewDidLoad() {
    super.viewDidLoad()

    let editButton = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(toggleEditing)) // create a bat button
    navigationItem.rightBarButtonItem = editButton // assign button
}

@objc private func toggleEditing() {
    listTableView.setEditing(!listTableView.isEditing, animated: true) // Set opposite value of current editing status
    navigationItem.rightBarButtonItem?.title = listTableView.isEditing ? "Done" : "Edit" // Set title depending on the editing status
}

Why do I think it's better:

为什么我认为它更好:

  1. Fewer code lines.
  2. 更少的代码行。

  3. Bar button is initialized once but not every time you press the button.
  4. 条形按钮初始化一次,但不是每按一次按钮。

#4


3  

Override the view controller's -setEditing:animated:, call super, and call the same method on your table view.

覆盖视图控制器的-setEditing:animated:,调用super,并在表视图上调用相同的方法。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    [self.tableView setEditing:editing animated:animated];
}

#5


1  

Swift 3.0 version of njuri post:

Swift 3.0版本的njuri帖子:

override func viewDidLoad() {
    super.viewDidLoad()

    PackageNameLabel.text = detailPackageName
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.plain, target: self, action: #selector(PackageDetailsTableViewController.editButtonPressed))

}


func editButtonPressed(){
    tableView.setEditing(!tableView.isEditing, animated: true)
    if tableView.isEditing == true{
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(PackageDetailsTableViewController.editButtonPressed))
    }else{
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.plain, target: self, action: #selector(PackageDetailsTableViewController.editButtonPressed))
    }
}

#6


1  

Here is a solution for Swift 4.2:

这是Swift 4.2的解决方案:

override func viewDidLoad() {
  super.viewDidLoad()

  // Use the edit button provided by the view controller.
  navigationItem.rightBarButtonItem = editButtonItem
}

override func setEditing(_ editing: Bool, animated: Bool) {
    // Takes care of toggling the button's title.
    super.setEditing(!isEditing, animated: true)

    // Toggle table view editing.
    tableView.setEditing(!tableView.isEditing, animated: true)
}

The view controller's setEditing is called by default when the editButtonItem is pressed. By default, pressing the button toggle's its title between "Edit" and "Done", so calling super.setEditing takes care of that for us, and we use the tableView's setEditing method to toggle the editing state of the table view.

按下editButtonItem时,默认情况下会调用视图控制器的setEditing。默认情况下,按下按钮在“编辑”和“完成”之间切换其标题,因此调用super.setEditing会为我们处理,我们使用tableView的setEditing方法切换表视图的编辑状态。

Sources:

#1


12  

Create rightBarButtonItem as below with an action.

使用操作创建如下所示的rightBarButtonItem。

In viewDidLoad() :

在viewDidLoad()中:

var rightButton = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("showEditing:"))
    self.navigationItem.rightBarButtonItem = rightButton

and then make a function like,

然后做一个像,

 func showEditing(sender: UIBarButtonItem)
 {
    if(self.tableView.editing == true)
    {
        self.tableView.editing = false
        self.navigationItem.rightBarButtonItem?.title = "Done"   
    }
    else
    {
        self.tableView.editing = true
        self.navigationItem.rightBarButtonItem?.title = "Edit" 
    }
}

Make sure, : is appended to function name in Selector of action in viewDidLoad
Hope it helps!

确保,:在viewDidLoad中的操作选择器中附加到函数名称希望它有所帮助!

#2


7  

Call this method on button click.

按下按钮调用此方法。

tableView.setEditing(true, animated: true)

Or if you want it to work like a toggle use

或者,如果您希望它像切换使用一样工作

tableView.setEditing(!tableView.editing, animated: true)

I assume you have a button, which calls editButtonPressed on press. So implementation of this method could look like this.

我假设你有一个按钮,在按下时调用editButtonPressed。因此,此方法的实现可能如下所示。

override func viewDidLoad(){
 navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editButtonPressed"))
}

func editButtonPressed(){
  tableView.setEditing(!tableView.editing, animated: true) 
  if tableView.editing == true{
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editButtonPressed"))
  }else{
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("editButtonPressed"))
  }
} 

This also changes title of the bar button.

这也改变了按钮的标题。

#3


7  

Swift 3 & 4 answer that IMHO is better than other answers:

Swift 3&4回答恕我直言比其他答案更好:

override func viewDidLoad() {
    super.viewDidLoad()

    let editButton = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(toggleEditing)) // create a bat button
    navigationItem.rightBarButtonItem = editButton // assign button
}

@objc private func toggleEditing() {
    listTableView.setEditing(!listTableView.isEditing, animated: true) // Set opposite value of current editing status
    navigationItem.rightBarButtonItem?.title = listTableView.isEditing ? "Done" : "Edit" // Set title depending on the editing status
}

Why do I think it's better:

为什么我认为它更好:

  1. Fewer code lines.
  2. 更少的代码行。

  3. Bar button is initialized once but not every time you press the button.
  4. 条形按钮初始化一次,但不是每按一次按钮。

#4


3  

Override the view controller's -setEditing:animated:, call super, and call the same method on your table view.

覆盖视图控制器的-setEditing:animated:,调用super,并在表视图上调用相同的方法。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    [self.tableView setEditing:editing animated:animated];
}

#5


1  

Swift 3.0 version of njuri post:

Swift 3.0版本的njuri帖子:

override func viewDidLoad() {
    super.viewDidLoad()

    PackageNameLabel.text = detailPackageName
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.plain, target: self, action: #selector(PackageDetailsTableViewController.editButtonPressed))

}


func editButtonPressed(){
    tableView.setEditing(!tableView.isEditing, animated: true)
    if tableView.isEditing == true{
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(PackageDetailsTableViewController.editButtonPressed))
    }else{
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle.plain, target: self, action: #selector(PackageDetailsTableViewController.editButtonPressed))
    }
}

#6


1  

Here is a solution for Swift 4.2:

这是Swift 4.2的解决方案:

override func viewDidLoad() {
  super.viewDidLoad()

  // Use the edit button provided by the view controller.
  navigationItem.rightBarButtonItem = editButtonItem
}

override func setEditing(_ editing: Bool, animated: Bool) {
    // Takes care of toggling the button's title.
    super.setEditing(!isEditing, animated: true)

    // Toggle table view editing.
    tableView.setEditing(!tableView.isEditing, animated: true)
}

The view controller's setEditing is called by default when the editButtonItem is pressed. By default, pressing the button toggle's its title between "Edit" and "Done", so calling super.setEditing takes care of that for us, and we use the tableView's setEditing method to toggle the editing state of the table view.

按下editButtonItem时,默认情况下会调用视图控制器的setEditing。默认情况下,按下按钮在“编辑”和“完成”之间切换其标题,因此调用super.setEditing会为我们处理,我们使用tableView的setEditing方法切换表视图的编辑状态。

Sources: