UITableVIew滑动尾随SwipeActionsConfigurationForRowAt不起作用

问题描述 投票:1回答:2

新iOS 11 UITableView滑动操作未调用。该表的delegatedatasource正常工作。

我无法滑动并查看菜单项。

下面是我的相同代码。

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let contextItem = UIContextualAction(style: .normal, title: "Leading & .normal") { (contextualAction, view, boolValue) in
        print("Leading Action style .normal")
    }
    let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])

    return swipeActions
}

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let contextItem = UIContextualAction(style: .destructive, title: "Trailing & .destructive") { (contextualAction, view, boolValue) in
        print("Trailing Action style .destructive")
    }
    let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])

    return swipeActions
}

我试图在下面打电话,并且可以正常工作。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

    }
}

任何向正确方向的提示都值得赞赏。

ios swift uitableview
2个回答
3
投票

您需要在闭包true中将UIContextualAction传递给boolValue(true)。否则处理程序将不允许该操作。

func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let contextItem = UIContextualAction(style: .normal, title: "Leading & .normal") { (contextualAction, view, boolValue) in
        boolValue(true) // pass true if you want the handler to allow the action
        print("Leading Action style .normal")
    }
    let swipeActions = UISwipeActionsConfiguration(actions: [contextItem])

    return swipeActions
}

0
投票

代码看起来不错,但您可能缺少Class声明中的“ UITableViewDelegate”以及viewdidload()部分中的“ TableView.delegate = self”。所以它应该看起来像:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

   override func viewDidLoad() {
      super.viewDidLoad()
      tableView.dataSource = self
      tableView.delegate = self
   }

}
© www.soinside.com 2019 - 2024. All rights reserved.