覆盖tableVIEw(_:commit editingStyle:forRowAt indexPath :).
除此之外,我已经完成了这项工作,而且我仍然没有刷卡删除功能.我试过的事情:
>在代码和IB中将tableVIEw.allowsMultipleSelectionDuringEditing设置为true和false.
>重写tableVIEw(_:canEditRowAt indexPath :)并返回true.
>重写tableVIEw(_:editingStyleForRowAt indexPath :)并返回.delete.
>以及上面的每一个组合.
我正在使用firebaseui和自定义UItableVIEwCell来填充表格.这是我的表视图控制器:
import UIKitimport FirebaseDatabaseUIclass 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?) { }}解决方法 最近的firebaseui更新打破了原来的答案.
更新的答案:
只需将FUItableVIEwDataSource子类化为实现自定义UItableVIEwDataSource功能,然后将子类绑定到UItableVIEw.
FUItableVIEwDataSource子类:
import UIKitimport FirebaseDatabaseUIclass 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 UIKitimport FirebaseDatabaseUIclass 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 } // 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?) { }}
原始答案:
解决方案是子类化FUItableVIEwDataSource并覆盖您想要的UItableVIEwDataSource方法.之后一切都很完美.
import UIKitimport FirebaseDatabaseUIclass 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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)