【问题标题】:Swift: How to enter edit mode for table?Swift:如何进入表格的编辑模式?
【发布时间】:2016-07-30 21:56:54
【问题描述】:

我有一个 RootViewController,它嵌入了一个包含表格的容器:

我希望 RootViewController 左上角的垃圾桶图标启用嵌入式表格视图的编辑模式。我希望它们显示为复选框,以便我可以一次选择多行,然后按“删除”删除所有选定的行。

我该怎么做?

【问题讨论】:

    标签: swift


    【解决方案1】:

    希望你的班级已经像这样符合UITableViewDelegate

    class MyViewController: UIViewController, UITableViewDelegate
    

    viewDidLoad() 中,您需要:

    myTable.delegate = self
    

    然后您可以将垃圾桶图标连接到将表格设置为编辑模式的 IBAction:

    @IBAction func myTableSetEditing(sender: AnyObject) {        
        myTable.setEditing(true, animated: true)   
    }
    

    然后,正如我们在此处的答案中看到的:Select multiple rows in tableview and tick the selected ones,在viewDidLoad() 中输入:

    self.tableView.allowsMultipleSelection = true
    

    并获得您的复选标记,实施:

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
    }
    
    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.None
    }
    

    【讨论】:

    • 如果我有通往包含表格的容器视图的出口,我可以从根控制器访问myTable 吗?
    • 哇好棒!!您的答案和该答案的组合使选择框显示
    • 现在我无法让它检查 didSelectRowAtIndexPath
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多