我有一个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 } }
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
希望它有所帮助!
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
希望它有所帮助!
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 }
为什么我认为它更好:
更少的代码行.
条形按钮初始化一次,但不是每按一次按钮.
这是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
的setEditing
方法来切换表格视图的编辑状态。
资料来源:
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
按下按钮调用此方法.
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")) } }
这也改变了按钮的标题.