【问题标题】:UITableView swipe-to-close in iOS 11iOS 11 中的 UITableView 滑动关闭
【发布时间】:2018-07-27 15:26:34
【问题描述】:

我在使用 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]
    }
}

【问题讨论】:

    标签: ios uitableview uitableviewrowaction


    【解决方案1】:

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

    如果有人对完整代码感兴趣,我已经更新了问题来源

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 2018-02-23
      • 2012-09-13
      • 1970-01-01
      相关资源
      最近更新 更多