FirebaseUI .
:
FUITableViewDataSource UITableViewDataSource, UITableView.
FUITableViewDataSource:
import UIKit
import FirebaseDatabaseUI
class EditableTableDataSource: FUITableViewDataSource {
typealias PopulateCellBlock = (UITableView, IndexPath, FIRDataSnapshot) -> UITableViewCell
typealias CommitEditBlock = (UITableView, UITableViewCellEditingStyle, IndexPath) -> Void
private let commitEditBlock: 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 {
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())
}
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
}
let snapshot = self.dataSource.snapshot(at: indexPath.row)
self.dataManager.moveToTrash(ScheduledHabit(fromSnapshot: snapshot))
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}
}
:
FUITableViewDataSource UITableViewDataSource, . .
import UIKit
import FirebaseDatabaseUI
class FUIEditableTableViewDataSource: FUITableViewDataSource {
typealias PopulateCellBlock = (UITableView, IndexPath, FIRDataSnapshot) -> UITableViewCell
typealias CommitEditBlock = (UITableView, UITableViewCellEditingStyle, IndexPath) -> Void
private let commitEditBlock: 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)
}
}
}