简体   繁体   English

Swift 3 - 支持“刷卡删除”的iOS 10 UITableView

[英]Swift 3 - iOS 10 UITableView enabling “swipe-to-delete”

There are a lot of questions about how to enable swipe-to-delete for a UITableView, and they all say the same thing: 关于如何为UITableView启用滑动删除有很多问题,他们都说同样的事情:

Override tableView(_:commit editingStyle:forRowAt indexPath:) . 覆盖tableView(_:commit editingStyle:forRowAt indexPath:)

I have done this, among other things, and I still do not have the swipe-to-delete functionality. 除此之外,我已经完成了这项工作,而且我仍然没有刷卡删除功能。 Things I've tried: 我试过的事情:

  1. Setting tableView.allowsMultipleSelectionDuringEditing to true and false, both in code and in IB. 在代码和IB中将tableView.allowsMultipleSelectionDuringEditing设置为true和false。
  2. Overriding tableView(_:canEditRowAt indexPath:) and returning true . 覆盖tableView(_:canEditRowAt indexPath:)并返回true
  3. Overriding tableView(_:editingStyleForRowAt indexPath:) and returning .delete . 覆盖tableView(_:editingStyleForRowAt indexPath:)并返回.delete
  4. And every combination of above. 以及上面的每一个组合。

I'm using FirebaseUI with a custom UITableViewCell to populate the table. 我正在使用FirebaseUI和自定义UITableViewCell来填充表格。 Here's my table view controller: 这是我的表视图控制器:

import UIKit
import FirebaseDatabaseUI

class ScheduleViewController: UITableViewController {

    private let TAG = String(describing: ScheduleViewController.self)

    private var dataSource: FUITableViewDataSource!

    override func viewDidLoad() {
        super.viewDidLoad()

        dataSource = self.tableView.bind(to: DataManager.instance.habitsQuery(),
                                         populateCell: populateCell())

        self.tableView.dataSource = dataSource

        // Automatically resize table cells to fit its content.
        self.tableView.estimatedRowHeight = ScheduleTableViewCell.HEIGHT
        self.tableView.rowHeight = UITableViewAutomaticDimension

        // I have also
        self.tableView.allowsMultipleSelectionDuringEditing = false
    }

    func populateCell() -> (UITableView, IndexPath, FIRDataSnapshot) -> UITableViewCell {
        return { tableView, indexPath, snapshot in
            let cell =
                tableView.dequeueReusableCell(withIdentifier: ScheduleTableViewCell.IDENTIFIER,
                                              for: indexPath) as! ScheduleTableViewCell

            if let dict = snapshot.value as? Dictionary<String, Any?> {
                cell.set(habit: Habit(withKey: snapshot.key, from: dict))
            } else {
                Log.e(self.TAG, "Invalid data returned from Firebase.")
            }

            return cell
        }
    }

    // MARK: TableView Delegate

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

    override func tableView(_ tableView: UITableView,
                            editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle
    {
        return .delete
    }

    override func tableView(_ tableView: UITableView,
                            commit editingStyle: UITableViewCellEditingStyle,
                            forRowAt indexPath: IndexPath)
    {

    }

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    }

}

Recent FirebaseUI updates have broken the original answer. 最近的FirebaseUI更新打破了原来的答案。

UPDATED ANSWER: 更新的答案:

Simply subclass FUITableViewDataSource to implement custom UITableViewDataSource functionality, then bind the subclass to your UITableView . 只需将FUITableViewDataSource子类FUITableViewDataSource实现自定义UITableViewDataSource功能,然后将子类绑定到UITableView

The FUITableViewDataSource subclass: FUITableViewDataSource子类:

import UIKit
import FirebaseDatabaseUI

class EditableTableDataSource: FUITableViewDataSource {

    /// Called to populate each cell in the UITableView.
    typealias PopulateCellBlock = (UITableView, IndexPath, FIRDataSnapshot) -> UITableViewCell

    /// Called to commit an edit to the UITableView.
    typealias CommitEditBlock = (UITableView, UITableViewCellEditingStyle, IndexPath) -> Void

    private let commitEditBlock: CommitEditBlock?

    /// A wrapper around FUITableViewDataSource.init(query:view tableView:populateCell:), with the
    /// addition of a CommitEditBlock.
    public init(query: FIRDatabaseQuery,
                populateCell: @escaping PopulateCellBlock,
                commitEdit: @escaping CommitEditBlock)
    {
        commitEditBlock = commitEdit
        super.init(collection: FUIArray.init(query: query), populateCell: populateCell)
    }

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

    override func tableView(_ tableView: UITableView,
                            commit editingStyle: UITableViewCellEditingStyle,
                            forRowAt indexPath: IndexPath)
    {
        if (commitEditBlock != nil) {
            commitEditBlock!(tableView, editingStyle, indexPath)
        }
    }

}

extension UITableView {

    /// Creates a data source, binds it to the table view, and returns it. Note that this is the
    /// `EditableTableViewDataSource` equivalent of the 
    /// `FUITableViewDataSource.bind(to:populateCell:)` method.
    ///
    /// - parameters:
    ///   - to:             The Firebase query to bind to.
    ///   - populateCell:   A closure that's called to populate each cell.
    ///   - commitEdit:     A closure that's called when the user commits some kind of edit. Maps to
    ///                     `tableView(:commit:forRowAt:)`.
    func bind(to query: FIRDatabaseQuery,
              populateCell: @escaping EditableTableDataSource.PopulateCellBlock,
              commitEdit: @escaping EditableTableDataSource.CommitEditBlock)
        -> EditableTableDataSource
    {
        let dataSource = EditableTableDataSource(query: query,
                                                 populateCell: populateCell,
                                                 commitEdit: commitEdit)
        dataSource.bind(to: self)
        return dataSource
    }

}

And usage: 用法:

import UIKit
import FirebaseDatabaseUI

class ScheduleViewController: UITableViewController {

    private let TAG = String(describing: ScheduleViewController.self)

    private var dataSource: FUITableViewDataSource!
    private var dataManager: DataManager!

    override func viewDidLoad() {
        super.viewDidLoad()

        dataManager = AppManager.defaultInstance.dataManager()

        dataSource = tableView.bind(
            to: dataManager.scheduledHabitsQuery(),
            populateCell: populateCellBlock(),
            commitEdit: commitEditBlock())
    }


    // MARK: TableView Data Source

    func populateCellBlock() -> EditableTableDataSource.PopulateCellBlock {
        return { tableView, indexPath, snapshot in
            let cell = ScheduledHabitTableViewCell.from(tableView: tableView, at: indexPath)
            cell.set(habit: ScheduledHabit(fromSnapshot: snapshot))
            return cell
        }
    }

    func commitEditBlock() -> EditableTableDataSource.CommitEditBlock {
        return { tableView, editingStyle, indexPath in
            if (editingStyle != .delete) {
                return
            }

            // Delete the data from Firebase.
            let snapshot = self.dataSource.snapshot(at: indexPath.row)
            self.dataManager.moveToTrash(ScheduledHabit(fromSnapshot: snapshot))

            // Deleting the table view row is done automatically by the FirebaseUI data source.
        }
    }


    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    }

}

ORIGINAL ANSWER: 原始答案:

The solution is to subclass FUITableViewDataSource and override the UITableViewDataSource methods you want. 解决方案是子类化FUITableViewDataSource并覆盖您想要的UITableViewDataSource方法。 Everything worked perfectly after that. 之后一切都很完美。

import UIKit
import FirebaseDatabaseUI

class FUIEditableTableViewDataSource: FUITableViewDataSource {

    /// Called to populate each cell in the UITableView.
    typealias PopulateCellBlock = (UITableView, IndexPath, FIRDataSnapshot) -> UITableViewCell

    /// Called to commit an edit to the UITableView.
    typealias CommitEditBlock = (UITableView, UITableViewCellEditingStyle, IndexPath) -> Void

    private let commitEditBlock: CommitEditBlock?

    /// A wrapper around FUITableViewDataSource.init(query:view tableView:populateCell:), with the
    /// addition of a CommitEditBlock.
    public init(query: FIRDatabaseQuery,
                tableView: UITableView,
                populateCell: @escaping PopulateCellBlock,
                commitEdit: @escaping CommitEditBlock)
    {
        commitEditBlock = commitEdit
        super.init(query: query, view: tableView, populateCell: populateCell)
    }

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

    override func tableView(_ tableView: UITableView,
                            commit editingStyle: UITableViewCellEditingStyle,
                            forRowAt indexPath: IndexPath)
    {
        if (commitEditBlock != nil) {
            commitEditBlock!(tableView, editingStyle, indexPath)
        }
    }

}
  1. On storyboard, select the view controller (the yellow thing on top of the view controller). 在情节提要板上,选择视图控制器(视图控制器顶部的黄色控件)。 While the view controller is selected: 选择视图控制器时:

Choose Editor > Embed In > Navigation Controller. 选择编辑器>嵌入>导航控制器。

  1. Add this to your viewDidLoad: 将其添加到viewDidLoad:

navigationItem.leftBarButtonItem = editButtonItem

So your viewDidLoad should look like: 所以你的viewDidLoad应该是这样的:

override func viewDidLoad() {
    super.viewDidLoad()

    // Use the edit button item provided by the table view controller.
    navigationItem.leftBarButtonItem = editButtonItem

    // Load the data.
}

Read this: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementEditAndDeleteBehavior.html#//apple_ref/doc/uid/TP40015214-CH9-SW1 阅读: https//developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementEditAndDeleteBehavior.html#//apple_ref/doc/uid/TP40015214-CH9-SW1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM