删除时Firebase / Swift更新表视图值?

时间:2016-07-07 08:36:43

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

我只是想知道如何在firebase中通过.observeEventType更新我的显示数据 - 以及我拥有的一系列字典。我有一个表视图,带有删除按钮功能。但是,我只能从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
        }



    }

此代码看起来相当混乱,但我只想在任何时候在顶部显示最高价格等级。删除值时,这不会更新。

1 个答案:

答案 0 :(得分:0)

您的代码存在一些设计问题,我将开始在您的案例中寻找更好的方法来处理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)
    }
}