iosuitableviewuitableviewrowaction

UITableView swipe-to-close in iOS 11


I have some trouble with UITableView swipe-to-close gesture to close actions in a cell. When I open cell actions it works fine, but when I'm trying to close it, it will close only after several trying.

I've tested it in a clean project and noticed that it is iOS 11 behaviour because in the previous iOS version these actions are closing by any touch outside action buttons zone.

How can I do the same behaviour?

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

Code. Just for a case

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

Solution

  • I've found the solution. Swipe-to-close works well in iOS 11 if you add leading actions.

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

    I've updated question source if someone interested in full code