Swift / 在视图控制器中启用编辑模式

Swift / Enable Editing Mode in View Controller

因为 UI 个元素,我创建了 View Controller 里面有一个 TableView。 但是我无法启用编辑模式。我尝试了几种没有解决方案的方法。 但是使用 TableView Controller 没有问题。

我尝试了什么:

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

哪里有问题?

您忘记将 table 视图置于编辑模式:

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

我假设你有 tableView 属性。根据需要进行调整。

您可能还想做一些其他事情来模拟 UITableViewController:

  1. viewWillAppear 中,您应该在 table 视图中取消选择当前选定的行。
  2. viewDidAppear 中,您应该闪烁 table 视图的滚动条。