当前位置:  开发笔记 > IOS > 正文

将表视图设置为编辑模式

如何解决《将表视图设置为编辑模式》经验,为你挑选了4个好方法。

我有一个UITableViewUIViewController并增加从代码,而不是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
  }
}

iRiziya.. 20

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" 
    }
}

确保,:Selector动作中附加功能名称viewDidLoad
希望它有所帮助!



1> iRiziya..:

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" 
    }
}

确保,:Selector动作中附加功能名称viewDidLoad
希望它有所帮助!


这种情况的区别就是为什么你应该只使用内置的`editButtonItem`.

2> Tung Fam..:

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
}

为什么我认为它更好:

    更少的代码行.

    条形按钮初始化一次,但不是每按一次按钮.



3> 小智..:

这是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将由我们来处理,我们使用tableViewsetEditing方法来切换表格视图的编辑状态。

资料来源:

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621471-editbuttonitem

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621378-setediting

https://developer.apple.com/documentation/uikit/uitableview/1614876-setediting



4> Juri Noga..:

按下按钮调用此方法.

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"))
  }
} 

这也改变了按钮的标题.

推荐阅读
雯颜哥_135
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有