Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iTableView在iOS 11中滑动关闭_Ios_Uitableview_Uitableviewrowaction - Fatal编程技术网

iTableView在iOS 11中滑动关闭

iTableView在iOS 11中滑动关闭,ios,uitableview,uitableviewrowaction,Ios,Uitableview,Uitableviewrowaction,我在使用UITableView滑动关闭手势关闭单元格中的操作时遇到一些问题。当我打开单元格操作时,它工作正常,但当我试图关闭它时,只有在多次尝试后它才会关闭 我在一个干净的项目中测试过它,注意到它是iOS 11的行为,因为在以前的iOS版本中,这些动作在动作按钮区域外的任何触摸都会关闭 我怎么能做出同样的行为 视频: 代码。只是为了一个案子 class ViewController: UIViewController { let tableView = UITableView(fram

我在使用UITableView滑动关闭手势关闭单元格中的操作时遇到一些问题。当我打开单元格操作时,它工作正常,但当我试图关闭它时,只有在多次尝试后它才会关闭

我在一个干净的项目中测试过它,注意到它是iOS 11的行为,因为在以前的iOS版本中,这些动作在动作按钮区域外的任何触摸都会关闭

我怎么能做出同样的行为

视频:

代码。只是为了一个案子

class ViewController: UIViewController {

    let tableView = UITableView(frame: .zero, style: .plain)

    var data = (0...3)
        .map { _ in
            return (0...5).map { _ in return false }
        }

    override func loadView() {
        super.loadView()
        view = tableView
    }

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

}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[section].count
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
        let isSelected = data[indexPath.section][indexPath.row]
        cell.textLabel?.text = "Section: \(indexPath.section) \nRow: \(indexPath.row)"
        cell.textLabel?.numberOfLines = 0
        cell.accessoryType = isSelected ? .checkmark : .none
        return cell
    }

    @available(iOS 11.0, *)
    func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let action = UIContextualAction(style: .normal, title: "Toggle 2") { [weak self] (action, view, handler) in
            guard let data = self?.data else { return }
            let isSelected = data[indexPath.section][indexPath.row]
            self?.data[indexPath.section][indexPath.row] = !isSelected
            self?.tableView.reloadRows(at: [indexPath], with: .fade)
            handler(true)
        }
        action.backgroundColor = .blue
        let configuration = UISwipeActionsConfiguration(actions: indexPath.section == 0 ? [action] : [])
        configuration.performsFirstActionWithFullSwipe = true
        return configuration
    }
}

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let action = UITableViewRowAction(style: .default,
                                          title: "Toggle") { [weak self] (action, indexPath) in
                                            guard let data = self?.data else { return }
                                            let isSelected = data[indexPath.section][indexPath.row]
                                            self?.data[indexPath.section][indexPath.row] = !isSelected
                                            self?.tableView.reloadRows(at: [indexPath], with: .fade)
        }
        return [action]
    }
}

我找到了解决办法。在iOS 11中,如果您添加了引导动作,则“滑动关闭”可以很好地工作

@available(iOS 11.0, *)
    func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let action = UIContextualAction(style: .normal, title: "Toggle 2") { [weak self] (action, view, handler) in
            guard let data = self?.data else { return }
            let isSelected = data[indexPath.section][indexPath.row]
            self?.data[indexPath.section][indexPath.row] = !isSelected
            self?.tableView.reloadRows(at: [indexPath], with: .fade)
            handler(true)
        }
        action.backgroundColor = .blue
        let configuration = UISwipeActionsConfiguration(actions: indexPath.section == 0 ? [action] : [])
        configuration.performsFirstActionWithFullSwipe = true
        return configuration
    }
如果有人对完整代码感兴趣,我已经更新了问题源代码