iOS 11中的UITableView滑动关闭

时间:2018-07-27 15:26:35

标签: ios uitableview uitableviewrowaction

使用UITableView滑动关闭手势来关闭单元格中的操作时遇到一些麻烦。当我打开单元格动作时,它可以正常工作,但是当我尝试关闭它时,只有经过几次尝试,它才会关闭。

我已经在一个干净的项目中对其进行了测试,并注意到它是iOS 11的行为,因为在以前的iOS版本中,这些操作通过外部操作按钮区域的任何触摸而关闭。

我该如何做同样的行为?

视频:https://www.youtube.com/watch?v=rKrk7q0HkeY

代码。只是为了一个案例

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

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。如果您添加前导动作,则滑动关闭功能在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
    }

如果有人对完整代码感兴趣,我会更新问题源