将表视图设置为编辑模式

时间:2015-12-08 13:59:11

标签: swift uitableview uiviewcontroller

我在UITableView中有一个UIViewController并且已经从代码而不是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
  }
}

7 个答案:

答案 0 :(得分:18)

通过操作创建rightBarButtonItem,如下所示。

viewDidLoad()

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

然后创建一个像

这样的函数
 func showEditing(sender: UIBarButtonItem)
 {
    if(self.tableView.isEditing == true)
    {
        self.tableView.isEditing = false
        self.navigationItem.rightBarButtonItem?.title = "Done"   
    }
    else
    {
        self.tableView.isEditing = true
        self.navigationItem.rightBarButtonItem?.title = "Edit" 
    }
}

确保:附加到SelectorviewDidLoad行动中的功能名称后附加HelloWorld.js 希望它有所帮助!

答案 1 :(得分:11)

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
}

为什么我认为更好

  1. 更少的代码行。
  2. 栏按钮初始化一次,但不是每按一次按钮。

答案 2 :(得分:7)

点击按钮调用此方法。

tableView.setEditing(true, animated: true)

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

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

我假设你有一个按钮,按下时会调出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"))
  }
} 

这也会改变小节按钮的标题。

答案 3 :(得分:6)

这是 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)
}

默认情况下,按下setEditing时将调用视图控制器的editButtonItem。默认情况下,按下按钮的标题会在“编辑”和“完成”之间切换,因此调用super.setEditing会为我们解决这个问题,我们使用tableView的{​​{1}}方法切换表格视图的编辑状态。

来源:

答案 4 :(得分:3)

覆盖视图控制器的-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版本的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)

第一:

let editButton = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(showEditing(_:)))
    navigationItem.rightBarButtonItem = editButton

然后:

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