swift - 如何在嵌入的 tableView 上实现编辑/删除行为?

标签 swift uitableview

所以我有一个嵌入式表格 View ,我想实现编辑/删除行为:Embedded tableview

在我的 HomePageViewController 中我有:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.leftBarButtonItem = editButtonItem
}

但是,发生的一切是当我单击编辑按钮时,它说完成,而嵌入的表格 View 根本不执行任何操作。当我将上面的代码放入 TableView 中时,没有任何反应。

如何让导航 Controller /父 View Controller 识别嵌入的表格 View ?

最佳答案

看起来您正在使用嵌入转场来嵌入 UITableViewController。在您的父 View Controller 中,您可以轻松地执行两件事中的一件来实现您的目标。

方法一:使用 subview Controller 的编辑按钮

override func viewDidLoad() {
    super.viewDidLoad()

    // Find and (optionally assign it to a variable for later convenience) the embedded controller, IBOutlets aren't available for VCs embedded within a storyboard
    let childControllers = childViewControllers.filter { return $0 is EventTableViewController }
    let embeddedController = childControllers[0] as! EventTableViewController

    navigationItem.leftBarButtonItem = embeddedController.editButtonItem
}

方法二:将编辑事件转发给 subview Controller

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = editButtonItem
}

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

    // Forward editing state to children
    childViewControllers.forEach { $0.setEditing(editing, animated: animated) }
}

注意:editButtonItem 已在 iOS 10 中公开(但 was implemented 更早)。对于低于 iOS 10 的部署目标,您可以结合使用自定义编辑按钮和方法 2。

关于swift - 如何在嵌入的 tableView 上实现编辑/删除行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45696084/

相关文章:

ios - Realm swift : Invalid value for property on xCode 9

ios - Swift:while 循环中的 UIAlertController

ios - 在 swift 2 中滚动时 Collection View 重用图像

ios - 将 UITextField 链接到 UITableViewCell 子类时出现问题

ios - 如何动态地制作具有多个部分的uitableview?

ios - 为什么 tableView 的 FooterView 在 vi​​ewDidLoad() 中不存在,如何解决?

UITableView 编辑/完成事件

iOS、 swift 、XCode : Why is the position of my elements different for different devices?

ios - Swift TableViewCell xib 没有约束

ios - 单击表格 View 单元格内的 View 创建弹出框 Segue