我對編碼領域相當陌生,我試圖用Firebase實現tableView。我試圖實現是:Firebase + Swift:無法刪除tableView中的行
- 使用的tableView,用戶可以輸入自己喜歡的位置
- 創建窗體(我用尤里卡這裏)供用戶挑選從某些選項創建一個「設置」頁面他們在設置頁面中輸入您的收藏夾
- 允許刪除那些「收藏夾」
爲了形式的工作,我拉在AppDelegate中通過user.uid過濾火力地堡數據。填充這些'收藏夾設置'。當我嘗試在'設置'處使用commitEdittingStyle刪除行時,它會拋出無效行數的錯誤。我嘗試搜索各種解決方案,並嘗試dispatch_async,但它不知何故只是不完全解決我的問題。我的一些代碼片段如下:
public var setUpFavLocations: [(String)] = []
public var favLocDatabase: [FIRDataSnapshot]! = []
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var ref: FIRDatabaseReference!
var _refHandle: FIRDatabaseHandle!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
let favLocVC: UITableViewController = FavLocationsTableViewController(style: UITableViewStyle.Plain)
ref = FIRDatabase.database().reference()
if let user = FIRAuth.auth()?.currentUser {
self.ref.child("settings").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
favLocDatabase.append(snapshot)
let insertionIndexPath = NSIndexPath(forRow: favLocDatabase.count - 1, inSection: 0)
favLocVC.tableView.insertRowsAtIndexPaths([insertionIndexPath], withRowAnimation: .Automatic)
})
self.ref.child("settings").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildAdded, withBlock: { (snapshot) in
setUpFavLocations.append(snapshot.value?["favLocation"] as! String)
})
//At FavLocationsTableViewController, aka at the settings page
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("count \(setUpFavLocations.count)")
return setUpFavLocations.count
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
if let user = FIRAuth.auth()?.currentUser {
let row = favLocDatabase[indexPath.row]
self.ref.child("settings").queryOrderedByChild("user").queryEqualToValue(user.uid).observeEventType(.ChildRemoved, withBlock: { (snapshot) -> Void in
favLocDatabase.removeAtIndex(indexPath.row)
self.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: indexPath.row, inSection: 0)], withRowAnimation: .Automatic)
})
row.ref.removeValue()
}
}
}
這樣做,這將引發我下面的錯誤
終止應用程序由於未捕獲的異常
「NSInternalInconsistencyException」,理由是:「無效的更新:無效 數(5)中的一個 現有節中包含的行數必須等於更新前(5)加上或減去 插入的行數或者d從該部分選擇(0插入, 1刪除)並加上或減去移入或移出 該部分(移入0,移出0)的行數。
請幫忙,我在這裏呆了好幾天。非常感激。
等等,你不是說你使用尤里卡?爲什麼不在這裏使用Eureka?這很容易! – Sweeper
@Sweeper我無法找到最適合此目的的Eureka單元。 – Koh
難道是你從數據庫中刪除了兩次該項目('removeAtIndex'和'removeValue')?爲什麼你創建一個新的索引路徑,而不是僅僅使用參數中傳遞的(實際上相同的)路徑? – vadian