gpt4 book ai didi

ios - Firebase + swift : Unable to delete rows in tableView

转载 作者:行者123 更新时间:2023-11-30 13:06:19 25 4
gpt4 key购买 nike

我对编码领域相当陌生,我正在尝试使用 Firebase 实现 tableView。我想要实现的是:

  1. 使用 tableView 创建“设置”页面,用户可以在其中输入他们喜欢的位置
  2. 创建一个表单(我在这里使用 Eureka),以便用户从他们在设置页面中输入的收藏夹中选择某些选项
  3. 允许删除这些“收藏夹”

为了使表单正常工作,我在 AppDelegate 中提取了按 user.uid 过滤的 Firebase 数据。填充这些“收藏夹设置”。当我尝试在“设置”中使用 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()
}

}
}

这样做会引发以下错误

Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

请帮忙,我已经被困在这里好几天了。非常感谢。

最佳答案

每次删除一行时都会创建一个新的监听器,这不太好。

尝试移动:

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)
})

与其他监听器一起进入您的 AppDelegate。

我还注意到您没有处理观察者,因此当您打开和关闭应用程序时,观察者将会重复。您应该在应用程序进入后台时删除观察者,并在应用程序进入前台时重新实例化它们。您将在 AppDelegate 中找到这些事件的方法。

关于ios - Firebase + swift : Unable to delete rows in tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39313704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com