Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift 3 – 支持“刷卡删除”的iOS 10 UITableView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
关于如何为UITableView启用滑动删除有很多问题,他们都说同样的事情:

覆盖tableView(_:commit ediTingStyle:forRowAt indexPath :).

除此之外,我已经完成了这项工作,而且我仍然没有刷卡删除功能.我试过的事情:

>在代码和IB中将tableView.allowsMultipleSELEctionDuringEdiTing设置为true和false.
>重写tableView(_:canEditRowAt indexPath :)并返回true.
>重写tableView(_:ediTingStyleForRowAt indexPath :)并返回.@R_616_9421@e.
>以及上面的每一个组合.

我正在使用firebaseui自定义UITableViewCell来填充表格.这是我的表视图控制器:

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 .@R_616_9421@e
    }

    override func tableView(_ tableView: UITableView,commit ediTingStyle: UITableViewCellEdiTingStyle,forRowAt indexPath: IndexPath)
    {

    }

    // MARK: - Navigation

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

}

解决方法

最近的firebaseui更新打破了原来的答案.

更新的答案:

只需将FUITableViewDatasource子类化为实现自定义UITableViewDatasource功能,然后将子类绑定到UITableView.

FUITableViewDatasource子类:

import UIKit
import FirebaseDatabaseUI

class EditableTableDatasource: FUITableViewDatasource {

    /// Called to populate each cell in the UITableView.
    typealias PopulateCellBlock = (UITableView,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,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
    }

}

用法

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,snapshot in
            let cell = scheduledHabitTableViewCell.from(tableView: tableView,at: indexPath)
            cell.set(habit: scheduledHabit(fromSnapshot: snapshot))
            return cell
        }
    }

    func commitEditBlock() -> EditableTableDatasource.CommitEditBlock {
        return { tableView,indexPath in
            if (ediTingStyle != .Delete) {
                return
            }

            // @R_616_9421@e the data from Firebase.
            let snapshot = self.datasource.snapshot(at: indexPath.row)
            self.dataManager.moveToTrash(scheduledHabit(fromSnapshot: snapshot))

            // @R_616_9421@ing the table view row is done automatically by the firebaseui data source.
        }
    }


    // MARK: - Navigation

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

}

原始答案:

解决方案是子类化FUITableViewDatasource并覆盖您想要的UITableViewDatasource方法.之后一切都很完美.

import UIKit
import FirebaseDatabaseUI

class FUIEditableTableViewDatasource: FUITableViewDatasource {

    /// Called to populate each cell in the UITableView.
    typealias PopulateCellBlock = (UITableView,tableView: UITableView,commitEdit: @escaping CommitEditBlock)
    {
        commitEditBlock = commitEdit
        super.init(query: query,view: tableView,indexPath)
        }
    }

}

大佬总结

以上是大佬教程为你收集整理的Swift 3 – 支持“刷卡删除”的iOS 10 UITableView全部内容,希望文章能够帮你解决Swift 3 – 支持“刷卡删除”的iOS 10 UITableView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。