【问题标题】:Firebase/Swift update table view value on delete?Firebase / Swift在删除时更新表视图值?
【发布时间】:2016-07-07 08:36:43
【问题描述】:

我只是想知道如何从 firebase 通过 .observeEventType 更新我在 Swift 中的显示数据 - 以及我拥有的一系列字典。我有一个带有删除按钮功能的表格视图。但是,我只能从 firebase 中删除值,而不是字典数组。我的 viewDidLoad 中有observeEventType,我的数据是这样的:

var dict = [String:String]()
dict["data"] = snapshot.value!["data"] as? String
array.append(dict)

但是,在 numberOfRowsInSection 中,我有 array.count,但我收到一个错误,指出计数错误。我将如何解决这个问题?从字典数组中删除值,将整个数据全部筛选并显示新数据,或者我该怎么办?

顺便说一句:当应用程序崩溃时,我再次构建它 - 已删除的值消失了,数据显示正常 - 但是,我只希望删除部分运行顺畅。

提前致谢。

完整代码:

func LoadValues() {


        var curID = FIRAuth.auth()?.currentUser?.uid

        ref.child("Users").child(curID!).child("Prices").observeEventType(.Value) { (snapshot:FIRDataSnapshot) in


            if let PermPrice = snapshot.value!["PermanentPrice"] as? Int {
                self.dict2["PermanentPrice"] = String(PermPrice)
            }

            if let Price24 = snapshot.value!["24hours"] as? Int {
                self.dict2["24hours"] = String(Price24)
            }

            if let Price12 = snapshot.value!["12hours"] as? Int {
                self.dict2["12hours"] = String(Price12)
            }

            if let Price6 = snapshot.value!["6hours"] as? Int {
                self.dict2["6hours"] = String(Price6)
            }

            if let Price2 = snapshot.value!["2hours"] as? Int {
                self.dict2["2hours"] = String(Price2)
            }

            self.allTiers.append(self.dict2)

        }

    }

上面的代码是一个价格等级系统,允许用户在 5 个不同的价格等级之间进行选择。这会显示在表格视图中。

这是在tableview中显示:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if indexPath.row == 0 {

            let cell1 = tableView.dequeueReusableCellWithIdentifier("firstCell", forIndexPath: indexPath) as! TierCell1

            cell1.deleteButton1.tag = 0

            cell1.deleteButton1.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)
            //let cell1 = tableView.dequeueReusableCellWithIdentifier("firstCell", forIndexPath: indexPath) as! TierCell1

            //set the data here

            if allTiers[0]["PermanentPrice"] != nil {
                cell1.price1.text = "$" + String(allTiers[0]["PermanentPrice"]!)
                cell1.text1.text = "Permanent"
                PermTextTier = true
            } else if allTiers[0]["24hours"] != nil {
                cell1.price1.text = "$" + String(allTiers[0]["24hours"]!)
                cell1.text1.text = "24 hours"
                TextTier24 = true
            } else if allTiers[0]["12hours"] != nil {
                cell1.price1.text = "$" + String(allTiers[0]["12hours"]!)
                cell1.text1.text = "12 hours"
                TextTier12 = true
            } else if allTiers[0]["6hours"] != nil {
                cell1.price1.text = "$" + String(allTiers[0]["6hours"]!)
                cell1.text1.text = "6 hours"
                TextTier6 = true
            } else if allTiers[0]["2hours"] != nil {
                cell1.price1.text = "$" + String(allTiers[0]["2hours"]!)
                cell1.text1.text = "2 hours"
                TextTier2 = true
            }

            //cell1.text1.text = String(allTiers[0]["PermanentPrice"]!)

            return cell1
        }
        if indexPath.row == 1 {
            //let cell2: TierCell2 = TierCell2(style: UITableViewCellStyle.Default, reuseIdentifier: "secondCell") as! TierCell2
            //set the data here


            let cell2 = tableView.dequeueReusableCellWithIdentifier("secondCell", forIndexPath: indexPath) as! TierCell2


            cell2.deleteButton2.tag = 1

            cell2.deleteButton2.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)


            if allTiers[0]["24hours"] != nil && TextTier24 == false {
                cell2.price2.text = "$" + String(allTiers[0]["24hours"]!)
                cell2.text2.text = "24 hours"
                TextTier24 = true
            } else if allTiers[0]["12hours"] != nil && TextTier12 == false {
                cell2.price2.text = "$" + String(allTiers[0]["12hours"]!)
                cell2.text2.text = "12 hours"
                TextTier12 = true
            } else if allTiers[0]["6hours"] != nil && TextTier6 == false {
                cell2.price2.text = "$" + String(allTiers[0]["6hours"]!)
                cell2.text2.text = "6 hours"
                TextTier6 = true
            } else if allTiers[0]["2hours"] != nil && TextTier2 == false {
                cell2.price2.text = "$" + String(allTiers[0]["2hours"]!)
                cell2.text2.text = "2 hours"
                TextTier2 = true
            }



            return cell2
        }
        if indexPath.row == 2 {
            //let cell2: TierCell2 = TierCell2(style: UITableViewCellStyle.Default, reuseIdentifier: "secondCell") as! TierCell2
            //set the data here

            let cell3 = tableView.dequeueReusableCellWithIdentifier("thirdCell", forIndexPath: indexPath) as! TierCell3

            cell3.deleteButton3.tag = 2

            cell3.deleteButton3.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)

            if allTiers[0]["12hours"] != nil && TextTier12 == false {
                cell3.price3.text = "$" + String(allTiers[0]["12hours"]!)
                cell3.text3.text = "12 hours"
                TextTier12 = true
            } else if allTiers[0]["6hours"] != nil && TextTier6 == false {
                cell3.price3.text = "$" + String(allTiers[0]["6hours"]!)
                cell3.text3.text = "6 hours"
                TextTier6 = true
            } else if allTiers[0]["2hours"] != nil && TextTier2 == false {
                cell3.price3.text = "$" + String(allTiers[0]["2hours"]!)
                cell3.text3.text = "2 hours"
                TextTier2 = true
            }


            return cell3
        }
        if indexPath.row == 3 {
            //let cell2: TierCell2 = TierCell2(style: UITableViewCellStyle.Default, reuseIdentifier: "secondCell") as! TierCell2
            //set the data here
            let cell4 = tableView.dequeueReusableCellWithIdentifier("fourthCell", forIndexPath: indexPath) as! TierCell4

            cell4.deleteButton4.tag = 3

            cell4.deleteButton4.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)

            if allTiers[0]["6hours"] != nil && TextTier6 == false {
                cell4.price4.text = "$" + String(allTiers[0]["6hours"]!)
                cell4.text4.text = "6 hours"
                TextTier6 = true
            } else if allTiers[0]["2hours"] != nil && TextTier2 == false {
                cell4.price4.text = "$" + String(allTiers[0]["2hours"]!)
                cell4.text4.text = "2 hours"
                TextTier2 = true
            }

            return cell4
        }
        if indexPath.row == 4 {
            //let cell2: TierCell2 = TierCell2(style: UITableViewCellStyle.Default, reuseIdentifier: "secondCell") as! TierCell2
            //set the data here
            let cell5 = tableView.dequeueReusableCellWithIdentifier("fifthCell", forIndexPath: indexPath) as! TierCell5

            cell5.deleteButton5.tag = 4

            cell5.deleteButton5.addTarget(self, action: "DeleteTier:", forControlEvents: .TouchUpInside)

            if allTiers[0]["2hours"] != nil && TextTier2 == false {
                cell5.price5.text = "$" + String(allTiers[0]["2hours"]!)
                cell5.text5.text = "2 hours"
                TextTier2 = true
            }


            return cell5
        } else {
            let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "none")


            return cell
        }



    }

这段代码看起来很乱,但我只想随时在顶部显示最高价格层。当我删除一个值时,这不会更新。

【问题讨论】:

  • 您可以将检索数据的firebase调用添加到tableview吗?
  • @adolfosrs 我相信问题出在 cellForRowAtIndexPath 上,但我想不出另一种方法..
  • 您是否碰巧在这里看到了解决方案,@adolfosrs?
  • 还是没搞清楚..

标签: arrays swift firebase tableview firebase-realtime-database


【解决方案1】:

您的代码存在一些设计问题,我将开始寻找更好的方法来处理您的情况下的 TableView。所以,请记住这一点。

使用您当前的 TableView 代码,如果我没有遗漏任何内容,那么为了实现您正在寻找的内容,您应该如何检索数据:

func LoadValues() {
    var curID = FIRAuth.auth()?.currentUser?.uid
    ref.child("Users").child(curID!).child("Prices").observeEventType(.Value, withBlock: { snapshot in
        if let PermPrice = snapshot.value!["PermanentPrice"] as? Int {
            self.dict2["PermanentPrice"] = String(PermPrice)
        }
        if let Price24 = snapshot.value!["24hours"] as? Int {
            self.dict2["24hours"] = String(Price24)
        }
        if let Price12 = snapshot.value!["12hours"] as? Int {
            self.dict2["12hours"] = String(Price12)
        }
        if let Price6 = snapshot.value!["6hours"] as? Int {
            self.dict2["6hours"] = String(Price6)
        }
        if let Price2 = snapshot.value!["2hours"] as? Int {
            self.dict2["2hours"] = String(Price2)
        }
        self.allTiers.removeAll()
        self.allTiers.append(self.dict2)
    }
}

【讨论】:

  • 这似乎不能解决我的问题。删除行后,表格视图会更新为错误的值。
  • @askaale 这就是我可以帮助你的方式。您可能在其他地方弄乱了您的数据。
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 2017-08-05
  • 2021-04-24
相关资源
最近更新 更多