Swift / Enable Editing Mode in View Controller

只谈情不闲聊 提交于 2019-12-03 09:45:14

问题


Because of UI elements, I created View Controller with a TableView inside. But I can't enable editing mode. I tried several methods without Solution. But with using TableView Controller there are no problems.

What I tried:

override func viewDidLoad() {
  self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func setEditing(editing: Bool, animated: Bool) {
 super.setEditing(editing, animated: animated)
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            // Action to delete data
        }
    }

Where can be the problem?


回答1:


You forgot to put the table view in editing mode:

override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    self.tableView.setEditing(editing, animated: animated)
}

I'm assuming you have a tableView property. Adjust as needed.

Some other things you may want to do to emulate a UITableViewController:

  1. In viewWillAppear you should deselect the currently selected row in the table view.
  2. In viewDidAppear you should flash the scrollbars of the table view.


来源:https://stackoverflow.com/questions/30688201/swift-enable-editing-mode-in-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!